Complete hashmaps exercise

This commit is contained in:
Suzie Kim 2024-02-01 11:08:03 -05:00
parent e527943d84
commit 6f2ce95f6e
No known key found for this signature in database
GPG key ID: 83C4CC3808F9AAE9
3 changed files with 22 additions and 7 deletions

View file

@ -11,17 +11,18 @@
// 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"), 3);
basket.insert(String::from("pear"), 5);
basket
}

View file

@ -14,8 +14,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)]
@ -40,6 +38,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// 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!
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 the goal details of a team.
@ -30,6 +28,7 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
for r in results.lines() {
let v: Vec<&str> = r.split(',').collect();
println!("result { }", r);
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();
@ -39,7 +38,23 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// will be the number of goals conceded by 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).or_insert(Team {
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).or_insert(Team {
goals_scored: 0,
goals_conceded: 0,
});
team_2.goals_scored += team_2_score;
team_2.goals_conceded += team_1_score;
};
scores
}