rustlings/exercises/11_hashmaps/hashmaps3.rs

78 lines
2.4 KiB
Rust
Raw Normal View History

// A list of scores (one per line) of a soccer match is given. Each line is of
2024-06-26 02:52:33 +03:00
// 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, the total
// number of goals the team scored, and the total number of goals the team
2024-06-26 02:52:33 +03:00
// conceded.
2021-06-04 13:39:12 +03:00
use std::collections::HashMap;
2023-06-08 17:46:45 +03:00
// A structure to store the goal details of a team.
2024-06-26 02:52:33 +03:00
#[derive(Default)]
2021-06-04 13:39:12 +03:00
struct Team {
goals_scored: u8,
goals_conceded: u8,
}
2024-06-26 02:52:33 +03:00
fn build_scores_table(results: &str) -> HashMap<&str, Team> {
2021-06-04 13:39:12 +03:00
// The name of the team is the key and its associated struct is the value.
2024-06-26 02:52:33 +03:00
let mut scores = HashMap::new();
for line in results.lines() {
let mut split_iterator = line.split(',');
// NOTE: We use `unwrap` because we didn't deal with error handling yet.
let team_1_name = split_iterator.next().unwrap();
let team_2_name = split_iterator.next().unwrap();
let team_1_score: u8 = split_iterator.next().unwrap().parse().unwrap();
let team_2_score: u8 = split_iterator.next().unwrap().parse().unwrap();
2021-06-04 13:39:12 +03:00
2024-06-26 02:52:33 +03:00
// TODO: Populate the scores table with the extracted details.
// 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.
2021-06-04 13:39:12 +03:00
}
2024-06-26 02:52:33 +03:00
2021-06-04 13:39:12 +03:00
scores
}
fn main() {
// You can optionally experiment here.
}
2021-06-04 13:39:12 +03:00
#[cfg(test)]
mod tests {
use super::*;
2024-06-26 02:52:33 +03:00
const RESULTS: &str = "England,France,4,2
France,Italy,3,1
Poland,Spain,2,0
Germany,England,2,1
England,Spain,1,0";
2021-06-04 13:39:12 +03:00
#[test]
fn build_scores() {
2024-06-26 02:52:33 +03:00
let scores = build_scores_table(RESULTS);
2021-06-04 13:39:12 +03:00
2024-06-26 02:52:33 +03:00
assert!(["England", "France", "Germany", "Italy", "Poland", "Spain"]
.into_iter()
.all(|team_name| scores.contains_key(team_name)));
2021-06-04 13:39:12 +03:00
}
#[test]
fn validate_team_score_1() {
2024-06-26 02:52:33 +03:00
let scores = build_scores_table(RESULTS);
2021-06-04 13:39:12 +03:00
let team = scores.get("England").unwrap();
2024-06-26 02:52:33 +03:00
assert_eq!(team.goals_scored, 6);
2021-06-04 13:39:12 +03:00
assert_eq!(team.goals_conceded, 4);
}
#[test]
fn validate_team_score_2() {
2024-06-26 02:52:33 +03:00
let scores = build_scores_table(RESULTS);
2021-06-04 13:39:12 +03:00
let team = scores.get("Spain").unwrap();
assert_eq!(team.goals_scored, 0);
2024-06-26 02:52:33 +03:00
assert_eq!(team.goals_conceded, 3);
2021-06-04 13:39:12 +03:00
}
}