From 3707571685af21423849df668b7bd5760ce1d2ac Mon Sep 17 00:00:00 2001 From: Hariettemaina Date: Thu, 18 May 2023 16:34:30 +0300 Subject: [PATCH] built scores table --- exercises/hashmaps/hashmaps3.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 18dd44c9..3af98d6b 100644 --- a/exercises/hashmaps/hashmaps3.rs +++ b/exercises/hashmaps/hashmaps3.rs @@ -25,6 +25,7 @@ struct Team { goals_conceded: u8, } + 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(); @@ -35,15 +36,29 @@ fn build_scores_table(results: String) -> HashMap { 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 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 team_1 = scores.entry(team_1_name.clone()).or_insert(Team { + name: team_1_name.clone(), + goals_scored: 0, + goals_conceded: 0, + }); + team_1.goals_scored += team_1_score; + team_1.goals_conceded += team_2_score; + + let team_2 = scores.entry(team_2_name.clone()).or_insert(Team { + name: team_2_name.clone(), + goals_scored: 0, + goals_conceded: 0, + }); + team_2.goals_scored += team_2_score; + team_2.goals_conceded += team_1_score; } + scores } + + #[cfg(test)] mod tests { use super::*;