Complete hashmap exercises

This commit is contained in:
ACvanWyk 2023-02-25 20:16:28 +02:00
parent 0bf936db98
commit 80213ce07e
3 changed files with 29 additions and 6 deletions

View File

@ -10,18 +10,17 @@
//
// 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<String, u32> {
let mut basket = // TODO: declare your hash map here.
let mut basket: HashMap<String, u32> = 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"), 2);
basket.insert(String::from("mango"), 2);
basket
}

View File

@ -11,8 +11,6 @@
//
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::collections::HashMap;
#[derive(Hash, PartialEq, Eq)]
@ -37,6 +35,9 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// TODO: Put new fruits if not already present. Note that you
// are not allowed to put any type of fruit that's already
// present!
if !basket.contains_key(&fruit) {
basket.insert(fruit, 2);
}
}
}

View File

@ -40,6 +40,29 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// 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.
scores
.entry(team_1_name.clone())
.and_modify(|team| {
team.goals_scored += team_1_score;
team.goals_conceded += team_2_score;
})
.or_insert(Team {
name: team_1_name,
goals_scored: team_1_score,
goals_conceded: team_2_score,
});
scores
.entry(team_2_name.clone())
.and_modify(|team| {
team.goals_scored += team_2_score;
team.goals_conceded += team_1_score;
})
.or_insert(Team {
name: team_2_name,
goals_scored: team_2_score,
goals_conceded: team_1_score,
});
}
scores
}