From 50adf98d5a72dc175710660780e7c8725d6ecfca Mon Sep 17 00:00:00 2001 From: Alex Sudbinin Date: Sun, 2 Oct 2022 22:13:45 -0700 Subject: [PATCH] hashmaps, modules, and quiz #2 --- exercises/hashmaps/hashmaps1.rs | 6 +++--- exercises/hashmaps/hashmaps2.rs | 5 +++-- exercises/hashmaps/hashmaps3.rs | 37 ++++++++++++++++++++++++++------- exercises/modules/modules1.rs | 4 +--- exercises/modules/modules2.rs | 7 ++----- exercises/modules/modules3.rs | 5 +---- exercises/quiz2.rs | 16 +++++++------- 7 files changed, 48 insertions(+), 32 deletions(-) diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index fd8dd2f8..977fa2ac 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -10,15 +10,15 @@ // // Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::collections::HashMap; fn fruit_basket() -> HashMap { - let mut basket = // TODO: declare your hash map here. + let mut basket: HashMap = HashMap::new(); // Two bananas are already given for you :) basket.insert(String::from("banana"), 2); + basket.insert(String::from("apple"), 3); + basket.insert(String::from("mango"), 4); // TODO: Put more fruits in your basket here. diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index 454b3e1d..10f11787 100644 --- a/exercises/hashmaps/hashmaps2.rs +++ b/exercises/hashmaps/hashmaps2.rs @@ -11,8 +11,6 @@ // // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::collections::HashMap; #[derive(Hash, PartialEq, Eq)] @@ -37,6 +35,9 @@ fn fruit_basket(basket: &mut HashMap) { // TODO: Put new fruits if not already present. Note that you // are not allowed to put any type of fruit that's already // present! + if !basket.contains_key(&fruit) { + basket.insert(fruit, 5); + } } } diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 18dd44c9..da919129 100644 --- a/exercises/hashmaps/hashmaps3.rs +++ b/exercises/hashmaps/hashmaps3.rs @@ -14,8 +14,6 @@ // Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::collections::HashMap; // A structure to store team name and its goal details. @@ -35,11 +33,36 @@ fn build_scores_table(results: String) -> HashMap { 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 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 team_1_updated_status = match scores.get(&team_1_name) { + Some(team) => Team { + name: team_1_name, + goals_scored: team.goals_scored + team_1_score, + goals_conceded: team.goals_conceded + team_2_score, + }, + None => Team { + name: team_1_name, + goals_scored: team_1_score, + goals_conceded: team_2_score, + }, + }; + + scores.insert(team_1_updated_status.name.clone(), team_1_updated_status); + + let team_2_updated_status = match scores.get(&team_2_name) { + Some(team) => Team { + name: team_2_name, + goals_scored: team.goals_scored + team_2_score, + goals_conceded: team.goals_conceded + team_1_score, + }, + None => Team { + name: team_2_name, + goals_scored: team_2_score, + goals_conceded: team_1_score, + }, + }; + + scores.insert(team_2_updated_status.name.clone(), team_2_updated_status); } scores } diff --git a/exercises/modules/modules1.rs b/exercises/modules/modules1.rs index 8dd0e402..09d5ab3e 100644 --- a/exercises/modules/modules1.rs +++ b/exercises/modules/modules1.rs @@ -1,15 +1,13 @@ // modules1.rs // Execute `rustlings hint modules1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - mod sausage_factory { // Don't let anybody outside of this module see this! fn get_secret_recipe() -> String { String::from("Ginger") } - fn make_sausage() { + pub fn make_sausage() { get_secret_recipe(); println!("sausage!"); } diff --git a/exercises/modules/modules2.rs b/exercises/modules/modules2.rs index c30a3897..33848947 100644 --- a/exercises/modules/modules2.rs +++ b/exercises/modules/modules2.rs @@ -3,12 +3,9 @@ // 'use' and 'as' keywords. Fix these 'use' statements to make the code compile. // Execute `rustlings hint modules2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - mod delicious_snacks { - // TODO: Fix these use statements - use self::fruits::PEAR as ??? - use self::veggies::CUCUMBER as ??? + pub use self::fruits::PEAR as fruit; + pub use self::veggies::CUCUMBER as veggie; mod fruits { pub const PEAR: &'static str = "Pear"; diff --git a/exercises/modules/modules3.rs b/exercises/modules/modules3.rs index 35e07990..f669ecf9 100644 --- a/exercises/modules/modules3.rs +++ b/exercises/modules/modules3.rs @@ -5,10 +5,7 @@ // from the std::time module. Bonus style points if you can do it with one line! // Execute `rustlings hint modules3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - -// TODO: Complete this use statement -use ??? +use std::time::{SystemTime, UNIX_EPOCH}; fn main() { match SystemTime::now().duration_since(UNIX_EPOCH) { diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index d8fa954a..e8441efe 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -18,8 +18,6 @@ // - The output element is going to be a Vector of strings. // Execute `rustlings hint quiz2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - pub enum Command { Uppercase, Trim, @@ -29,12 +27,15 @@ pub enum Command { 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(size) => output.push(string.to_owned() + &"bar".repeat(*size)), + } } output } @@ -42,8 +43,7 @@ mod my_module { #[cfg(test)] mod tests { - // TODO: What do we have to import to have `transformer` in scope? - use ???; + use super::my_module::transformer; use super::Command; #[test]