Skip to content

Commit

Permalink
11 hashmap exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
rganesan committed May 15, 2024
1 parent 7bbd6d7 commit 906b10f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
8 changes: 4 additions & 4 deletions exercises/11_hashmaps/hashmaps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,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::new();

// Two bananas are already given for you :)
basket.insert(String::from("banana"), 2);

// TODO: Put more fruits in your basket here.
// Put more fruits in your basket here.
basket.insert(String::from("apples"), 3);
basket.insert(String::from("mango"), 3);

basket
}
Expand Down
5 changes: 2 additions & 3 deletions exercises/11_hashmaps/hashmaps2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -37,9 +35,10 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
];

for fruit in fruit_kinds {
// TODO: Insert new fruits if they are not already present in the
// 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);
}
}

Expand Down
14 changes: 11 additions & 3 deletions exercises/11_hashmaps/hashmaps3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,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.
Expand All @@ -35,11 +33,21 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
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
// Populate the scores table with details extracted from the
// current line. Keep in mind that goals scored by team_1
// 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.
scores.entry(team_1_name).and_modify(|team| {
team.goals_scored += team_1_score;
team.goals_conceded += team_2_score;
}).or_insert(Team{ 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(Team{ goals_scored: team_2_score,
goals_conceded: team_1_score });
}
scores
}
Expand Down

0 comments on commit 906b10f

Please sign in to comment.