Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyYee committed Oct 16, 2024
1 parent 7461ebc commit 4fbc611
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
7 changes: 4 additions & 3 deletions exercises/hashmaps/hashmaps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
// 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> = Default::default();// 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("pear"), 2);

basket
}

Expand Down
16 changes: 14 additions & 2 deletions exercises/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 @@ -40,6 +38,20 @@ 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!

match fruit {
Fruit::Banana => {
basket.insert(Fruit::Banana, 2);
},

Fruit::Pineapple => {
basket.insert(Fruit::Pineapple, 2);
},

_ => {

}
}
}
}

Expand Down
18 changes: 15 additions & 3 deletions exercises/hashmaps/hashmaps3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// A list of scores (one per line) of a soccer match is given. Each line is of
// the form : "<team_1_name>,<team_2_name>,<team_1_goals>,<team_2_goals>"
// Example: England,France,4,2 (England scored 4 goals, France 2).
//

// You have to build a scores table containing the name of the team, goals the
// team scored, and goals the team conceded. One approach to build the scores
// table is to use a Hashmap. The solution is partially written to use a
Expand All @@ -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,20 @@ 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.

let team1 = scores.entry(team_1_name).or_insert(Team {
goals_scored: 0,
goals_conceded: 0
});
team1.goals_scored += team_1_score;
team1.goals_conceded += team_2_score;

let team2 = scores.entry(team_2_name).or_insert(Team {
goals_scored: 0,
goals_conceded: 0
});
team2.goals_scored += team_2_score;
team2.goals_conceded += team_1_score;
}
scores
}
Expand Down

0 comments on commit 4fbc611

Please sign in to comment.