From d722b0c2e38419726b5d2809ab0776f44280b525 Mon Sep 17 00:00:00 2001 From: Abrar Habib Date: Thu, 30 Mar 2023 15:15:21 -0400 Subject: [PATCH] finished quiz 2 and started options --- exercises/hashmaps/hashmaps3.rs | 19 +++++++++++++++---- exercises/options/options1.rs | 20 +++++++++++++++++--- exercises/options/options2.rs | 2 +- exercises/quiz2.rs | 18 ++++++++++++++---- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 5178be41..9b15bc3e 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; @@ -40,12 +39,24 @@ 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. - scores.entry(team_1_name).or_insert(Team{ + // If the key doesn't exist, initialize the goals to be 0. then add the goals. + + let score1 = scores.entry(team_1_name.clone()).or_insert(Team{ name: team_1_name, - goals_scored: team_1_score, - goals_conceded: team_2_score + goals_scored: 0, + goals_conceded: 0 }); + score1.goals_scored += team_1_score; + score1.goals_conceded += team_2_score; + let score2 = scores.entry(team_2_name.clone()).or_insert(Team{ + name: team_2_name, + goals_scored: 0, + goals_conceded: 0 + }); + score2.goals_scored += team_2_score; + score2.goals_conceded += team_1_score; + } scores } diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs index 1f891b0e..0010d12c 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -1,7 +1,8 @@ // options1.rs // Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE + +use std::time; // 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 @@ -10,7 +11,20 @@ fn maybe_icecream(time_of_day: u16) -> Option { // We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a 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! - ??? + + // match time_of_day { + // 0..=21 => Some(5), + // 22..=24 => Some(0), + // _ => None, + // } + + if time_of_day < 22 { + Some(5) + } else if time_of_day < 24 { + Some(0) + } else { + None + } } #[cfg(test)] @@ -30,6 +44,6 @@ mod tests { fn raw_value() { // 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(5)); } } diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs index 4e36443f..5b6c6406 100644 --- a/exercises/options/options2.rs +++ b/exercises/options/options2.rs @@ -11,7 +11,7 @@ mod tests { let optional_target = Some(target); // TODO: Make this an if let statement whose value is "Some" type - word = optional_target { + if let Some(word) = target { assert_eq!(word, target); } } diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 5c42dae0..4049144c 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -18,7 +18,6 @@ // - The output element is going to be a Vector of strings. // No hints this time! -// I AM NOT DONE pub enum Command { Uppercase, @@ -30,21 +29,32 @@ 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().to_string()), + Command::Append(n) => output.push(push_bar(string, *n)), + Command::Trim => output.push(string.trim().to_string()), + } } output } + + fn push_bar(string: &str, n: usize) -> String { + let bar = "bar".repeat(n); + let mut res = String::from(string.to_string() + &bar); + res + } } #[cfg(test)] mod tests { // TODO: What do we need to import to have `transformer` in scope? - use ???; use super::Command; + use super::my_module::transformer; #[test] fn it_works() {