From 5ec35b41d5436ae835a727ad232d0275c573581e Mon Sep 17 00:00:00 2001 From: catwithtudou <949812478@qq.com> Date: Wed, 25 Oct 2023 10:56:30 +0800 Subject: [PATCH] update:done the quiz2 and options1 --- exercises/hashmaps/hashmaps1.rs | 6 +++--- exercises/hashmaps/hashmaps2.rs | 5 ++--- exercises/hashmaps/hashmaps3.rs | 14 ++++++++++++-- exercises/options/options1.rs | 12 ++++++++---- exercises/quiz2.rs | 23 +++++++++++++++++------ 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index 80829eaa..18031e34 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -11,15 +11,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::new(); // Two bananas are already given for you :) basket.insert(String::from("banana"), 2); + basket.insert(String::from("apple"), 3); + basket.insert(String::from("mange"), 3); // TODO: Put more fruits in your basket here. diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index a5925690..ac05b48b 100644 --- a/exercises/hashmaps/hashmaps2.rs +++ b/exercises/hashmaps/hashmaps2.rs @@ -14,8 +14,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,7 @@ fn fruit_basket(basket: &mut HashMap) { ]; for fruit in fruit_kinds { + basket.entry(fruit).or_insert(1); // 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! @@ -81,7 +80,7 @@ mod tests { let count = basket.values().sum::(); assert!(count > 11); } - + #[test] fn all_fruit_types_in_basket() { let mut basket = get_fruit_basket(); diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 08e977c3..f7e3cedd 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 the goal details of a team. @@ -34,6 +32,18 @@ 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(); + let team1 = scores.entry(team_1_name).or_insert(Team { + goals_scored: 0, + goals_conceded: 0, + }); + team1.goals_scored += team_1_score; + team1.goals_conceded += team_2_score; + let team2 = scores.entry(team_2_name).or_insert(Team { + goals_scored: 0, + goals_conceded: 0, + }); + team2.goals_scored += team_2_score; + team2.goals_conceded += team_1_score; // TODO: Populate the scores table with details extracted from the // current line. Keep in mind that goals scored by team_1 // will be the number of goals conceded from team_2, and similarly diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs index e131b48b..323b2f47 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -3,8 +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 // all, so there'll be no more left :( @@ -13,7 +11,13 @@ 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 < 22 { + return Some(5); + } + if time_of_day >= 22 && time_of_day <= 24 { + return Some(0); + } + None } #[cfg(test)] @@ -34,6 +38,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(5)); } } diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 29925caf..7aef7caa 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -20,8 +20,6 @@ // // No hints this time! -// I AM NOT DONE - pub enum Command { Uppercase, Trim, @@ -32,11 +30,25 @@ 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 => { + let transformed_string = string.to_uppercase(); + output.push(transformed_string); + } + Command::Trim => { + let transformed_string = string.trim().to_string(); + output.push(transformed_string); + } + Command::Append(n) => { + let appended_string = format!("{}{}", string, "bar".repeat(*n)); + output.push(appended_string); + } + } } output } @@ -44,8 +56,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::*; use super::Command; #[test]