Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added people type to alias vec of persons for later use #36

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gift_circle"
version = "0.8.5"
version = "0.9.0"
edition = "2021"
authors = ["Chuck King"]

Expand Down
52 changes: 26 additions & 26 deletions src/gift_circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use counter::Counter;
use rand::seq::SliceRandom;

use super::group::Group;
use super::people::People;
use super::person::Person;

fn largest_group(persons: &[Person]) -> Group {
Expand Down Expand Up @@ -70,20 +71,20 @@ fn is_gift_circle_valid(persons: &[Person]) -> bool {
first_and_last_groups_are_different(persons) && has_no_consecutive_group_numbers(persons)
}

fn move_person(from_persons: &mut Vec<Person>, to_persons: &mut Vec<Person>, person: &Person) {
fn move_person(from_persons: &mut People, to_persons: &mut People, person: &Person) {
from_persons.retain(|p| p != person);
to_persons.push(person.clone());
}

fn generate_path(from_persons: &[Person]) -> Vec<Person> {
fn generate_path(from_persons: &[Person]) -> People {
// Go through the list of available participants and generate a gift path
// where noone gives a gift to anyone in their same group.

// Preserve the from_persons vec for follow on attempts by working with a cloned vec
let mut available_persons: Vec<Person> = from_persons.to_owned();
let mut available_persons: People = from_persons.to_owned();

// Build up the path by adding persons with different group numbers
let mut persons_path: Vec<Person> = vec![];
let mut persons_path: People = vec![];

let mut previous_group: u16 = 0;

Expand All @@ -93,22 +94,21 @@ fn generate_path(from_persons: &[Person]) -> Vec<Person> {

let largest_np_group = largest_non_prev_group(&available_persons, previous_group);

let candidates: Vec<Person> =
if (largest_np_group.size as usize * 2) > available_persons.len() {
// Build candidates list from the remaining persons in the largest group that is not the previous group
available_persons
.iter()
.filter(|&p| p.group_number.unwrap() == largest_np_group.number)
.cloned()
.collect::<Vec<Person>>()
} else {
// Build the candidates list from all remaining persons not in the previous group
available_persons
.iter()
.filter(|&p| p.group_number.unwrap() != previous_group)
.cloned()
.collect::<Vec<Person>>()
};
let candidates: People = if (largest_np_group.size as usize * 2) > available_persons.len() {
// Build candidates list from the remaining persons in the largest group that is not the previous group
available_persons
.iter()
.filter(|&p| p.group_number.unwrap() == largest_np_group.number)
.cloned()
.collect::<People>()
} else {
// Build the candidates list from all remaining persons not in the previous group
available_persons
.iter()
.filter(|&p| p.group_number.unwrap() != previous_group)
.cloned()
.collect::<People>()
};

// Randomly select one person from the candidates list
let choice = candidates.choose(&mut rand::thread_rng()).unwrap();
Expand All @@ -122,14 +122,14 @@ fn generate_path(from_persons: &[Person]) -> Vec<Person> {
persons_path
}

fn generate_no_group_path(from_persons: &[Person]) -> Vec<Person> {
fn generate_no_group_path(from_persons: &[Person]) -> People {
// Go through the list of available participants and generate a gift path.

// Preserve the from_persons vec for follow on attempts by working with a cloned vec
let mut available_persons: Vec<Person> = from_persons.to_owned();
let mut available_persons: People = from_persons.to_owned();

// Build up the path by adding random persons
let mut persons_path: Vec<Person> = vec![];
let mut persons_path: People = vec![];

while !available_persons.is_empty() {
// Randomly select one person
Expand All @@ -145,7 +145,7 @@ fn generate_no_group_path(from_persons: &[Person]) -> Vec<Person> {
persons_path
}

pub fn get_gift_circle(from_persons: Vec<Person>, use_groups: bool) -> Result<Vec<Person>> {
pub fn get_gift_circle(from_persons: People, use_groups: bool) -> Result<People> {
if from_persons.len() <= 2 {
return Err(anyhow!(
"You must submit at least three people in order to form a gift circle."
Expand All @@ -170,7 +170,7 @@ pub fn get_gift_circle(from_persons: Vec<Person>, use_groups: bool) -> Result<Ve
let mut attempt_count: u16 = 0;

let mut have_valid_circle = false;
let mut gift_circle: Vec<Person> = vec![];
let mut gift_circle: People = vec![];

while !have_valid_circle && attempt_count < MAX_ATTEMPTS {
if use_groups {
Expand Down Expand Up @@ -328,7 +328,7 @@ mod tests {
let person1 = Person::new("Father", 1);
let person_to_move = person1.clone();
let mut move_from = vec![person1];
let mut move_to: Vec<Person> = vec![];
let mut move_to = vec![];
move_person(&mut move_from, &mut move_to, &person_to_move);
assert_eq!(move_from.len(), 0);
assert_eq!(move_to.len(), 1);
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod gift_circle;
mod group;
mod myargs;
mod people;
mod person;

use std::io;
Expand All @@ -10,6 +11,7 @@ use anyhow::{anyhow, Context, Result};

use gift_circle::get_gift_circle;
use myargs::get_args;
use people::People;
use person::{Person, PersonWithoutGroup};

fn run() -> Result<()> {
Expand All @@ -18,15 +20,15 @@ fn run() -> Result<()> {
let mut rdr = csv::Reader::from_path(args.input.clone())
.with_context(|| format!("Failed to read input from {}", &args.input))?;

let mut participants: Vec<Person> = vec![];
let mut participants: People = vec![];

for result in rdr.deserialize() {
let person: Person = result?;
participants.push(person);
}

#[allow(unused_assignments)]
let mut gift_circle: Vec<Person> = vec![];
let mut gift_circle: People = vec![];

if args.use_groups {
if participants.iter().any(|p| p.group_number.is_none()) {
Expand Down
4 changes: 4 additions & 0 deletions src/people.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use crate::Person;

#[allow(dead_code)]
pub type People = Vec<Person>;
Loading