From 2657ce87521b7ac0fd18ff3c48623598be7a37a7 Mon Sep 17 00:00:00 2001 From: ext-daniyalov-dg Date: Mon, 20 Nov 2023 11:59:50 +0300 Subject: [PATCH] hashmaps and quiz --- exercises/11_hashmaps/hashmaps3.rs | 16 +++++++++++----- exercises/quiz2.rs | 20 ++++++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/exercises/11_hashmaps/hashmaps3.rs b/exercises/11_hashmaps/hashmaps3.rs index 7a930e0c..068fe7b1 100644 --- a/exercises/11_hashmaps/hashmaps3.rs +++ b/exercises/11_hashmaps/hashmaps3.rs @@ -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 { // The name of the team is the key and its associated struct is the value. let mut scores: HashMap = HashMap::new(); @@ -40,10 +46,10 @@ fn build_scores_table(results: String) -> HashMap { // 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 { }); } }; - 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 { diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 29925caf..fa4fedfe 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -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 { // TODO: Complete the output declaration! - let mut output: ??? = vec![]; + let mut output: Vec = 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]