mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 13:19:18 +00:00
fmt
This commit is contained in:
parent
6d22e3a2bb
commit
48350051bc
@ -18,7 +18,6 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
// A structure to store team name and its goal details.
|
||||
struct Team {
|
||||
name: String,
|
||||
goals_scored: u8,
|
||||
@ -26,67 +25,42 @@ struct Team {
|
||||
}
|
||||
|
||||
fn build_scores_table(results: String) -> HashMap<String, Team> {
|
||||
// The name of the team is the key and its associated struct is the value.
|
||||
let mut scores: HashMap<String, Team> = HashMap::new();
|
||||
let mut scores = HashMap::new();
|
||||
for result in results.lines() {
|
||||
let (team_1, team_2) = parse_teams(result);
|
||||
update_or_insert(&mut scores, team_1);
|
||||
update_or_insert(&mut scores, team_2);
|
||||
}
|
||||
scores
|
||||
}
|
||||
|
||||
for r in results.lines() {
|
||||
let v: Vec<&str> = r.split(',').collect();
|
||||
let team_1_name = v[0].to_string();
|
||||
let team_1_score: u8 = v[2].parse().unwrap();
|
||||
let team_2_name = v[1].to_string();
|
||||
let team_2_score: u8 = v[3].parse().unwrap();
|
||||
// TODO: Populate the scores table with details extracted from the
|
||||
// current line. Keep in mind that goals scored by team_1
|
||||
// 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 t1: Team = Team {
|
||||
name: team_1_name.clone(),
|
||||
fn parse_teams(result: &str) -> (Team, Team) {
|
||||
let result = result.split(',').collect::<Vec<&str>>();
|
||||
let team_1_name = result[0].to_string();
|
||||
let team_1_score = result[2].parse().unwrap();
|
||||
let team_2_name = result[1].to_string();
|
||||
let team_2_score = result[3].parse().unwrap();
|
||||
let team_1 = Team {
|
||||
name: team_1_name,
|
||||
goals_scored: team_1_score,
|
||||
goals_conceded: team_2_score,
|
||||
};
|
||||
|
||||
let t2: Team = Team {
|
||||
name: team_2_name.clone(),
|
||||
let team_2 = Team {
|
||||
name: team_2_name,
|
||||
goals_scored: team_2_score,
|
||||
goals_conceded: team_1_score,
|
||||
};
|
||||
(team_1, team_2)
|
||||
}
|
||||
|
||||
let t1 = scores
|
||||
.entry(team_1_name)
|
||||
.and_modify(|ot1| {
|
||||
ot1.goals_scored += t1.goals_scored;
|
||||
ot1.goals_conceded += t1.goals_conceded;
|
||||
})
|
||||
.or_insert(t1);
|
||||
|
||||
let t2 = scores
|
||||
.entry(team_2_name)
|
||||
.and_modify(|ot2| {
|
||||
ot2.goals_scored += t2.goals_scored;
|
||||
ot2.goals_conceded += t2.goals_conceded;
|
||||
})
|
||||
.or_insert(t2);
|
||||
|
||||
// match scores.get_mut(&team_1_name){
|
||||
// Some(ot1) => {
|
||||
// ot1.goals_scored += t1.goals_scored;
|
||||
// ot1.goals_conceded += t1.goals_conceded;
|
||||
// },
|
||||
// None => {scores.insert(team_1_name, t1);},
|
||||
// }
|
||||
//
|
||||
// match scores.get_mut(&team_2_name){
|
||||
// Some(ot2) => {
|
||||
// ot2.goals_scored += t2.goals_scored;
|
||||
// ot2.goals_conceded += t2.goals_conceded;
|
||||
// },
|
||||
// None => {scores.insert(team_2_name, t2);},
|
||||
// }
|
||||
//
|
||||
}
|
||||
fn update_or_insert(scores: &mut HashMap<String, Team>, team: Team) {
|
||||
scores
|
||||
.entry(team.name.clone())
|
||||
.and_modify(|t| {
|
||||
t.goals_scored += team.goals_scored;
|
||||
t.goals_conceded += team.goals_conceded;
|
||||
})
|
||||
.or_insert(team);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -105,7 +79,6 @@ mod tests {
|
||||
#[test]
|
||||
fn build_scores() {
|
||||
let scores = build_scores_table(get_results());
|
||||
|
||||
let mut keys: Vec<&String> = scores.keys().collect();
|
||||
keys.sort();
|
||||
assert_eq!(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user