From a4f9fc297a536767c7dcafe18b52ca9c1981f0a6 Mon Sep 17 00:00:00 2001 From: ben1009 Date: Wed, 15 Feb 2023 18:41:14 +0800 Subject: [PATCH] done till exercises/options/options1 --- exercises/hashmaps/README.md | 1 + exercises/hashmaps/hashmaps1.rs | 5 +++-- exercises/hashmaps/hashmaps2.rs | 2 +- exercises/hashmaps/hashmaps3.rs | 38 +++++++++++++++++++++++++++++++-- exercises/modules/modules1.rs | 4 +--- exercises/modules/modules2.rs | 5 ++--- exercises/modules/modules3.rs | 4 ++-- exercises/quiz2.rs | 19 ++++++++++++----- exercises/strings/strings4.rs | 1 + 9 files changed, 61 insertions(+), 18 deletions(-) diff --git a/exercises/hashmaps/README.md b/exercises/hashmaps/README.md index 30471cf9..0bd28bb8 100644 --- a/exercises/hashmaps/README.md +++ b/exercises/hashmaps/README.md @@ -1,4 +1,5 @@ # Hashmaps + A *hash map* allows you to associate a value with a particular key. You may also know this by the names [*unordered map* in C++](https://en.cppreference.com/w/cpp/container/unordered_map), [*dictionary* in Python](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) or an *associative array* in other languages. diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index fd8dd2f8..9e86562e 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -10,17 +10,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); // TODO: Put more fruits in your basket here. + basket.insert(String::from("apple"), 3); + basket.insert(String::from("apple1"), 3); basket } diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index 454b3e1d..59715613 100644 --- a/exercises/hashmaps/hashmaps2.rs +++ b/exercises/hashmaps/hashmaps2.rs @@ -11,7 +11,6 @@ // // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE use std::collections::HashMap; @@ -37,6 +36,7 @@ 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! + basket.entry(fruit).or_insert(1); } } diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index ad3baa68..f8ef0ca8 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,42 @@ 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. + + let team1 = scores.get_mut(&team_1_name); + match team1 { + Some(t) => { + t.goals_scored += team_1_score; + t.goals_conceded += team_2_score; + } + None => { + scores.insert( + team_1_name.clone(), + Team { + name: team_1_name.clone(), + goals_scored: team_1_score, + goals_conceded: team_2_score, + }, + ); + } + } + + let team2 = scores.get_mut(&team_2_name); + match team2 { + Some(t) => { + t.goals_scored += team_2_score; + t.goals_conceded += team_1_score; + } + None => { + scores.insert( + team_2_name.clone(), + Team { + name: team_2_name.clone(), + goals_scored: team_2_score, + goals_conceded: team_1_score, + }, + ); + } + } } 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..3092f278 100644 --- a/exercises/modules/modules2.rs +++ b/exercises/modules/modules2.rs @@ -3,12 +3,11 @@ // '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..fc61c326 100644 --- a/exercises/modules/modules3.rs +++ b/exercises/modules/modules3.rs @@ -5,10 +5,10 @@ // 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 5c42dae0..5fa9ef5d 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,22 @@ 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![]; for (string, command) in input.iter() { // TODO: Complete the function body. You can do it! + match command { + Command::Append(u) => { + output.push(format!("{}{}", string, "bar".repeat(*u))); + } + Command::Trim => { + output.push(string.trim().to_string()); + } + Command::Uppercase => { + output.push(string.to_uppercase().to_string()); + } + } } output } @@ -43,7 +52,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] diff --git a/exercises/strings/strings4.rs b/exercises/strings/strings4.rs index bcf756e1..0c5b6312 100644 --- a/exercises/strings/strings4.rs +++ b/exercises/strings/strings4.rs @@ -9,6 +9,7 @@ fn string_slice(arg: &str) { println!("{}", arg); } + fn string(arg: String) { println!("{}", arg); }