feat: completed solutions

This commit is contained in:
2026-03-23 03:36:33 -04:00
parent 2279bea6f1
commit f568c094cb
65 changed files with 424 additions and 139 deletions
+40 -4
View File
@@ -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]