mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 21:29:18 +00:00
fmt
This commit is contained in:
parent
6d22e3a2bb
commit
48350051bc
@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
// A structure to store team name and its goal details.
|
|
||||||
struct Team {
|
struct Team {
|
||||||
name: String,
|
name: String,
|
||||||
goals_scored: u8,
|
goals_scored: u8,
|
||||||
@ -26,69 +25,44 @@ struct Team {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn build_scores_table(results: String) -> HashMap<String, 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::new();
|
||||||
let mut scores: HashMap<String, Team> = HashMap::new();
|
for result in results.lines() {
|
||||||
|
let (team_1, team_2) = parse_teams(result);
|
||||||
for r in results.lines() {
|
update_or_insert(&mut scores, team_1);
|
||||||
let v: Vec<&str> = r.split(',').collect();
|
update_or_insert(&mut scores, team_2);
|
||||||
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(),
|
|
||||||
goals_scored: team_1_score,
|
|
||||||
goals_conceded: team_2_score,
|
|
||||||
};
|
|
||||||
|
|
||||||
let t2: Team = Team {
|
|
||||||
name: team_2_name.clone(),
|
|
||||||
goals_scored: team_2_score,
|
|
||||||
goals_conceded: team_1_score,
|
|
||||||
};
|
|
||||||
|
|
||||||
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);},
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
scores
|
scores
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 team_2 = Team {
|
||||||
|
name: team_2_name,
|
||||||
|
goals_scored: team_2_score,
|
||||||
|
goals_conceded: team_1_score,
|
||||||
|
};
|
||||||
|
(team_1, team_2)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -105,7 +79,6 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn build_scores() {
|
fn build_scores() {
|
||||||
let scores = build_scores_table(get_results());
|
let scores = build_scores_table(get_results());
|
||||||
|
|
||||||
let mut keys: Vec<&String> = scores.keys().collect();
|
let mut keys: Vec<&String> = scores.keys().collect();
|
||||||
keys.sort();
|
keys.sort();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user