From 7a49d00ae726fc0792625b14c25dabe52355c0b2 Mon Sep 17 00:00:00 2001 From: matytan <-f> Date: Sat, 19 Nov 2022 10:23:51 +0800 Subject: [PATCH] day4 --- exercises/hashmaps/hashmaps1.rs | 6 +++--- exercises/hashmaps/hashmaps2.rs | 4 ++-- exercises/hashmaps/hashmaps3.rs | 17 +++++++++++++++-- exercises/options/options1.rs | 10 ++++++---- exercises/options/options2.rs | 15 +++++++++------ exercises/options/options3.rs | 6 ++---- exercises/quiz2.rs | 26 +++++++++++++++++++++----- 7 files changed, 58 insertions(+), 26 deletions(-) diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index fd8dd2f8..3ca6255d 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -10,17 +10,17 @@ // // 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); // TODO: Put more fruits in your basket here. + basket.insert(String::from("apple"), 3); + basket.insert(String::from("pear"), 4); basket } diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index 454b3e1d..a4f6fdb3 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,8 @@ 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! + let count = basket.entry(fruit).or_insert(1); + // *count += 1; } } diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 18dd44c9..8791cb4c 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. @@ -40,6 +38,21 @@ fn build_scores_table(results: String) -> HashMap { // 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 team1 = scores.entry(team_1_name.clone()).or_insert(Team { + name: team_1_name, + 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.clone()).or_insert(Team { + name: team_2_name, + goals_scored: 0, + goals_conceded: 0, + }); + + team2.goals_scored += team_2_score; + team2.goals_conceded += team_1_score; } scores } diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs index 1149af08..77b630ac 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -1,8 +1,6 @@ // options1.rs // 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 :( @@ -10,7 +8,11 @@ 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. - ??? + match time_of_day { + time if time <= 10 => Some(5), + time if time < 25 => Some(0), + _ => None, + } } #[cfg(test)] @@ -30,6 +32,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(0)); } } diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs index b1120471..8b923128 100644 --- a/exercises/options/options2.rs +++ b/exercises/options/options2.rs @@ -1,8 +1,6 @@ // options2.rs // Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[cfg(test)] mod tests { use super::*; @@ -13,8 +11,8 @@ mod tests { let optional_target = Some(target); // TODO: Make this an if let statement whose value is "Some" type - word = optional_target { - assert_eq!(word, target); + if let word = optional_target { + assert_eq!(word.unwrap(), target); } } @@ -25,11 +23,16 @@ mod tests { for i in 0..(range + 1) { optional_integers.push(Some(i)); } + println!("{}", optional_integers.len()); // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option // You can stack `Option`'s into while let and if let - integer = optional_integers.pop() { - assert_eq!(integer, range); + while let integer = optional_integers.pop() { + //Option> vec.pop()本身会新套一层 option + if integer == None { + break; + }; + assert_eq!(integer.unwrap().unwrap(), range); range -= 1; } } diff --git a/exercises/options/options3.rs b/exercises/options/options3.rs index 3f995c52..f52fd087 100644 --- a/exercises/options/options3.rs +++ b/exercises/options/options3.rs @@ -1,8 +1,6 @@ // options3.rs // Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - struct Point { x: i32, y: i32, @@ -12,8 +10,8 @@ fn main() { let y: Option = Some(Point { x: 100, y: 200 }); match y { - Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), + Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y), _ => println!("no match"), } - y; // Fix without deleting this line. + y; // Fix without deleting this line. 已经部分移动到 p 中 } diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 606d3c70..d3c1a6d9 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -18,8 +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, Trim, @@ -30,11 +28,29 @@ 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 s = string.to_uppercase(); + output.push(s.to_string()); + } + Command::Trim => { + let string = string.trim().to_string(); + output.push(string); + } + Command::Append(size) => { + let mut string: String = string.into(); + + for _ in 0..*size { + string.push_str("bar"); + } + output.push(string.to_string()); + } + } } output } @@ -43,8 +59,8 @@ mod my_module { #[cfg(test)] mod tests { // TODO: What do we have to import to have `transformer` in scope? - use ???; use super::Command; + use my_module::transformer; #[test] fn it_works() {