diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 1733ff1c..4acdc14f 100644 --- a/exercises/hashmaps/hashmaps3.rs +++ b/exercises/hashmaps/hashmaps3.rs @@ -18,7 +18,6 @@ use std::collections::HashMap; -// A structure to store team name and its goal details. struct Team { name: String, goals_scored: u8, @@ -26,69 +25,44 @@ struct Team { } fn build_scores_table(results: String) -> HashMap { - // The name of the team is the key and its associated struct is the value. - let mut scores: HashMap = HashMap::new(); - - for r in results.lines() { - let v: Vec<&str> = r.split(',').collect(); - let team_1_name = v[0].to_string(); - let team_1_score: u8 = v[2].parse().unwrap(); - let team_2_name = v[1].to_string(); - let team_2_score: u8 = v[3].parse().unwrap(); - // TODO: Populate the scores table with details extracted from the - // current line. Keep in mind that goals scored by team_1 - // will be the number of goals conceded from team_2, and similarly - // goals scored by team_2 will be the number of goals conceded by - // team_1. - - let t1: Team = Team { - name: team_1_name.clone(), - goals_scored: team_1_score, - goals_conceded: team_2_score, - }; - - let t2: Team = Team { - name: team_2_name.clone(), - goals_scored: team_2_score, - goals_conceded: team_1_score, - }; - - let t1 = scores - .entry(team_1_name) - .and_modify(|ot1| { - ot1.goals_scored += t1.goals_scored; - ot1.goals_conceded += t1.goals_conceded; - }) - .or_insert(t1); - - let t2 = scores - .entry(team_2_name) - .and_modify(|ot2| { - ot2.goals_scored += t2.goals_scored; - ot2.goals_conceded += t2.goals_conceded; - }) - .or_insert(t2); - - // match scores.get_mut(&team_1_name){ - // Some(ot1) => { - // ot1.goals_scored += t1.goals_scored; - // ot1.goals_conceded += t1.goals_conceded; - // }, - // None => {scores.insert(team_1_name, t1);}, - // } - // - // match scores.get_mut(&team_2_name){ - // Some(ot2) => { - // ot2.goals_scored += t2.goals_scored; - // ot2.goals_conceded += t2.goals_conceded; - // }, - // None => {scores.insert(team_2_name, t2);}, - // } - // + let mut scores = HashMap::new(); + for result in results.lines() { + let (team_1, team_2) = parse_teams(result); + update_or_insert(&mut scores, team_1); + update_or_insert(&mut scores, team_2); } scores } +fn parse_teams(result: &str) -> (Team, Team) { + let result = result.split(',').collect::>(); + let team_1_name = result[0].to_string(); + let team_1_score = result[2].parse().unwrap(); + let team_2_name = result[1].to_string(); + let team_2_score = result[3].parse().unwrap(); + let team_1 = Team { + name: team_1_name, + goals_scored: team_1_score, + goals_conceded: team_2_score, + }; + let team_2 = Team { + name: team_2_name, + goals_scored: team_2_score, + goals_conceded: team_1_score, + }; + (team_1, team_2) +} + +fn update_or_insert(scores: &mut HashMap, team: Team) { + scores + .entry(team.name.clone()) + .and_modify(|t| { + t.goals_scored += team.goals_scored; + t.goals_conceded += team.goals_conceded; + }) + .or_insert(team); +} + #[cfg(test)] mod tests { use super::*; @@ -105,7 +79,6 @@ mod tests { #[test] fn build_scores() { let scores = build_scores_table(get_results()); - let mut keys: Vec<&String> = scores.keys().collect(); keys.sort(); assert_eq!(