feat: completed solutions
This commit is contained in:
@@ -8,10 +8,12 @@ use std::collections::HashMap;
|
||||
|
||||
fn fruit_basket() -> HashMap<String, u32> {
|
||||
// TODO: Declare the hash map.
|
||||
// let mut basket =
|
||||
let mut basket = HashMap::<String, u32>::new();
|
||||
|
||||
// Two bananas are already given for you :)
|
||||
basket.insert(String::from("banana"), 2);
|
||||
basket.insert(String::from("apple"), 2);
|
||||
basket.insert(String::from("mango"), 2);
|
||||
|
||||
// TODO: Put more fruits in your basket.
|
||||
|
||||
|
||||
@@ -32,6 +32,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,13 +31,47 @@ fn build_scores_table(results: &str) -> HashMap<&str, TeamScores> {
|
||||
// Keep in mind that goals scored by team 1 will be the number of goals
|
||||
// conceded by team 2. Similarly, goals scored by team 2 will be the
|
||||
// number of goals conceded by team 1.
|
||||
}
|
||||
scores
|
||||
.entry(team_1_name)
|
||||
.and_modify(|current_team_1_scores| {
|
||||
current_team_1_scores.goals_scored += team_1_score;
|
||||
current_team_1_scores.goals_conceded += team_2_score
|
||||
})
|
||||
.or_insert(TeamScores {
|
||||
goals_scored: team_1_score,
|
||||
goals_conceded: team_2_score,
|
||||
});
|
||||
|
||||
scores
|
||||
.entry(team_2_name)
|
||||
.and_modify(|current_team_2_scores| {
|
||||
current_team_2_scores.goals_scored += team_2_score;
|
||||
current_team_2_scores.goals_conceded += team_1_score
|
||||
})
|
||||
.or_insert(TeamScores {
|
||||
goals_scored: team_2_score,
|
||||
goals_conceded: team_1_score,
|
||||
});
|
||||
}
|
||||
scores
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
const RESULTS: &str = "England,France,4,2
|
||||
France,Italy,3,1
|
||||
Poland,Spain,2,0
|
||||
Germany,England,2,1
|
||||
England,Spain,1,0";
|
||||
|
||||
let scores = build_scores_table(RESULTS);
|
||||
|
||||
for (key, value) in &scores {
|
||||
println!(
|
||||
"{key}: (scored: {0}, conceeded: {1})",
|
||||
value.goals_scored, value.goals_conceded
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -54,9 +88,11 @@ England,Spain,1,0";
|
||||
fn build_scores() {
|
||||
let scores = build_scores_table(RESULTS);
|
||||
|
||||
assert!(["England", "France", "Germany", "Italy", "Poland", "Spain"]
|
||||
.into_iter()
|
||||
.all(|team_name| scores.contains_key(team_name)));
|
||||
assert!(
|
||||
["England", "France", "Germany", "Italy", "Poland", "Spain"]
|
||||
.into_iter()
|
||||
.all(|team_name| scores.contains_key(team_name))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user