From 4af39ccfdea24ffce76bdbe421e49812d480e059 Mon Sep 17 00:00:00 2001 From: "David E. Perez Negron R." Date: Sat, 4 Nov 2023 13:03:25 -0600 Subject: [PATCH] hashmaps done --- exercises/hashmaps/hashmaps1.rs | 5 +++-- exercises/hashmaps/hashmaps2.rs | 5 ++++- exercises/hashmaps/hashmaps3.rs | 30 +++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index 80829ea..c7adfa4 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -11,18 +11,19 @@ // Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE use std::collections::HashMap; fn fruit_basket() -> HashMap { - let mut basket = // TODO: declare your hash map here. + let mut basket = HashMap::new(); // TODO: declare your hash map here. // Two bananas are already given for you :) basket.insert(String::from("banana"), 2); // TODO: Put more fruits in your basket here. + basket.insert(String::from("apple"), 3); + basket.insert(String::from("mango"), 5); basket } diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index a592569..25139f1 100644 --- a/exercises/hashmaps/hashmaps2.rs +++ b/exercises/hashmaps/hashmaps2.rs @@ -14,7 +14,6 @@ // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE use std::collections::HashMap; @@ -40,7 +39,11 @@ fn fruit_basket(basket: &mut HashMap) { // TODO: Insert new fruits if they are not already present in the // basket. Note that you are not allowed to put any type of fruit that's // already present! + if fruit == Fruit::Banana || fruit == Fruit::Pineapple { + basket.insert(fruit, 1); + } } + } #[cfg(test)] diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 08e977c..fc0c8b2 100644 --- a/exercises/hashmaps/hashmaps3.rs +++ b/exercises/hashmaps/hashmaps3.rs @@ -14,7 +14,6 @@ // Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE use std::collections::HashMap; @@ -39,6 +38,35 @@ fn build_scores_table(results: String) -> HashMap { // 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 mut team_1 = Team{ + goals_scored: team_1_score, + goals_conceded: team_2_score + }; + + let mut team_2 = Team{ + goals_scored: team_2_score, + goals_conceded: team_1_score + }; + + match scores.get(&team_1_name) { + None => {scores.insert(team_1_name, team_1);}, + + Some(t) => { + team_1.goals_scored += t.goals_scored; + team_1.goals_conceded += t.goals_conceded; + scores.insert(team_1_name, team_1); + } + }; + + match scores.get(&team_2_name) { + None => {scores.insert(team_2_name, team_2);}, + + Some(t) => { + team_2.goals_scored += t.goals_scored; + team_2.goals_conceded += t.goals_conceded; + scores.insert(team_2_name, team_2); + } + }; } scores }