🦀 HashMap Solved

This commit is contained in:
asif256000 2023-01-12 01:13:21 -05:00
parent b0df00caad
commit 52d47e1d2f
3 changed files with 29 additions and 7 deletions

View File

@ -10,17 +10,20 @@
//
// 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::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("grapes"), 6);
basket.insert(String::from("kiwi"), 4);
basket.insert(String::from("avocado"), 2);
basket.insert(String::from("orange"), 1);
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,7 @@ 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!
basket.entry(fruit).or_insert(1);
}
}

View File

@ -14,8 +14,6 @@
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::collections::HashMap;
// A structure to store team name and its goal details.
@ -40,6 +38,28 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// 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.
scores
.entry(team_1_name)
.and_modify(|team| {
team.goals_scored += team_1_score;
team.goals_conceded += team_2_score;
})
.or_insert_with_key(|team_name| Team {
name: team_name.to_string(),
goals_scored: team_1_score,
goals_conceded: team_2_score,
});
scores
.entry(team_2_name)
.and_modify(|team| {
team.goals_scored += team_2_score;
team.goals_conceded += team_1_score;
})
.or_insert_with_key(|team_name| Team {
name: team_name.to_string(),
goals_scored: team_2_score,
goals_conceded: team_1_score,
});
}
scores
}