hashmaps and quiz

This commit is contained in:
ext-daniyalov-dg 2023-11-20 11:59:50 +03:00
parent 49cb2480e7
commit 2657ce8752
2 changed files with 27 additions and 9 deletions

View File

@ -14,7 +14,6 @@
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
use std::collections::HashMap;
@ -25,6 +24,13 @@ struct Team {
goals_conceded: u8,
}
impl Team {
fn add_scores(&mut self, scored: &u8, conceded: &u8){
self.goals_scored += scored;
self.goals_conceded += conceded;
}
}
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();
@ -40,10 +46,10 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// 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 team1 = scores.get(&team_1_name);
let team1 = scores.get_mut(&team_1_name);
match team1 {
Some(value) => {
value.goals_scored += team_1_score;
value.add_scores(&team_1_score, &team_2_score);
println!("{:?}", value);
},
None => {
@ -53,10 +59,10 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
});
}
};
let team2 = scores.get(&team_2_name);
let team2 = scores.get_mut(&team_2_name);
match team2 {
Some(value) => {
println!("{:?}", value);
value.add_scores(&team_2_score, &team_1_score);
},
None => {
scores.insert(team_2_name, Team {

View File

@ -20,7 +20,6 @@
//
// No hints this time!
// I AM NOT DONE
pub enum Command {
Uppercase,
@ -32,11 +31,24 @@ mod my_module {
use super::Command;
// TODO: Complete the function signature!
pub fn transformer(input: ???) -> ??? {
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
// TODO: Complete the output declaration!
let mut output: ??? = vec![];
let mut output: Vec<String> = vec![];
for (string, command) in input.iter() {
// TODO: Complete the function body. You can do it!
match command {
Command::Uppercase => {
output.push(string.to_uppercase());
},
Command::Trim => {
output.push(string.trim().to_string());
},
Command::Append(val) => {
let mut new_string = string.clone();
new_string.push_str(&"bar".repeat(*val));
output.push(new_string.to_string());
}
}
}
output
}
@ -45,7 +57,7 @@ mod my_module {
#[cfg(test)]
mod tests {
// TODO: What do we need to import to have `transformer` in scope?
use ???;
use super::my_module::transformer;
use super::Command;
#[test]