Skip to content

Commit

Permalink
Merge pull request #11 from Joaopmorais/hashmap
Browse files Browse the repository at this point in the history
hashmap exercises
  • Loading branch information
Joaopmorais committed Feb 19, 2024
2 parents f595e5d + 3c5c665 commit 1ebfb0c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
7 changes: 3 additions & 4 deletions exercises/11_hashmaps/hashmaps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
// 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);

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

basket
Expand Down
6 changes: 3 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 @@ -34,12 +32,14 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
Fruit::Mango,
Fruit::Lychee,
Fruit::Pineapple,
];
];

for fruit in fruit_kinds {
// 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);

}
}

Expand Down
14 changes: 12 additions & 2 deletions exercises/11_hashmaps/hashmaps3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -39,6 +37,18 @@ 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.
scores.entry(team_1_name).and_modify(
|t|{
t.goals_scored += team_1_score;
t.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(
|t|{
t.goals_scored += team_2_score;
t.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 1ebfb0c

Please sign in to comment.