From c29bf477b73ba992a84233b1405ff6ff36d6e5ef Mon Sep 17 00:00:00 2001 From: Tirth Patel Date: Mon, 11 Mar 2024 00:14:10 -0230 Subject: [PATCH] more exercise --- exercises/08_enums/enums3.rs | 12 ++++++++++-- exercises/09_strings/strings1.rs | 3 +-- exercises/09_strings/strings2.rs | 3 +-- exercises/09_strings/strings3.rs | 8 ++++---- exercises/09_strings/strings4.rs | 21 ++++++++++----------- exercises/10_modules/modules1.rs | 3 +-- exercises/10_modules/modules2.rs | 5 ++--- exercises/10_modules/modules3.rs | 3 +-- exercises/11_hashmaps/hashmaps1.rs | 6 +++--- exercises/11_hashmaps/hashmaps2.rs | 4 +++- exercises/11_hashmaps/hashmaps3.rs | 7 ++++++- exercises/12_options/options1.rs | 10 +++++++--- exercises/12_options/options2.rs | 5 ++--- exercises/12_options/options3.rs | 3 +-- exercises/quiz2.rs | 12 ++++++++---- 15 files changed, 60 insertions(+), 45 deletions(-) diff --git a/exercises/08_enums/enums3.rs b/exercises/08_enums/enums3.rs index 92d18c46..e0b16204 100644 --- a/exercises/08_enums/enums3.rs +++ b/exercises/08_enums/enums3.rs @@ -5,10 +5,12 @@ // Execute `rustlings hint enums3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE enum Message { - // TODO: implement the message variant types based on their usage below + ChangeColor(u8,u8,u8), + Echo(String), + Move(Point), + Quit } struct Point { @@ -44,6 +46,12 @@ impl State { // TODO: create a match expression to process the different message variants // Remember: When passing a tuple as a function argument, you'll need extra parentheses: // fn function((t, u, p, l, e)) + match message { + Message::ChangeColor(r,g,b) => self.color = (r,g,b), + Message::Echo(test) => self.message = test, + Message::Move(test) => self.position = test, + Message::Quit => self.quit = true + } } } diff --git a/exercises/09_strings/strings1.rs b/exercises/09_strings/strings1.rs index f50e1fa9..723e986b 100644 --- a/exercises/09_strings/strings1.rs +++ b/exercises/09_strings/strings1.rs @@ -5,13 +5,12 @@ // Execute `rustlings hint strings1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn main() { let answer = current_favorite_color(); println!("My current favorite color is {}", answer); } -fn current_favorite_color() -> String { +fn current_favorite_color() -> &'static str { "blue" } diff --git a/exercises/09_strings/strings2.rs b/exercises/09_strings/strings2.rs index 4d95d16a..7941cddc 100644 --- a/exercises/09_strings/strings2.rs +++ b/exercises/09_strings/strings2.rs @@ -5,7 +5,6 @@ // Execute `rustlings hint strings2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn main() { let word = String::from("green"); // Try not changing this line :) @@ -16,6 +15,6 @@ fn main() { } } -fn is_a_color_word(attempt: &str) -> bool { +fn is_a_color_word(attempt: String) -> bool { attempt == "green" || attempt == "blue" || attempt == "red" } diff --git a/exercises/09_strings/strings3.rs b/exercises/09_strings/strings3.rs index b29f9325..ab9f2fe7 100644 --- a/exercises/09_strings/strings3.rs +++ b/exercises/09_strings/strings3.rs @@ -3,21 +3,21 @@ // Execute `rustlings hint strings3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn trim_me(input: &str) -> String { // TODO: Remove whitespace from both ends of a string! - ??? + String::from(input.trim()) } fn compose_me(input: &str) -> String { // TODO: Add " world!" to the string! There's multiple ways to do this! - ??? + let rv = String::from(input) + " world!"; + rv } fn replace_me(input: &str) -> String { // TODO: Replace "cars" in the string with "balloons"! - ??? + input.replace("cars","balloons") } #[cfg(test)] diff --git a/exercises/09_strings/strings4.rs b/exercises/09_strings/strings4.rs index e8c54acc..a368fa63 100644 --- a/exercises/09_strings/strings4.rs +++ b/exercises/09_strings/strings4.rs @@ -7,7 +7,6 @@ // // No hints this time! -// I AM NOT DONE fn string_slice(arg: &str) { println!("{}", arg); @@ -17,14 +16,14 @@ fn string(arg: String) { } fn main() { - ???("blue"); - ???("red".to_string()); - ???(String::from("hi")); - ???("rust is fun!".to_owned()); - ???("nice weather".into()); - ???(format!("Interpolation {}", "Station")); - ???(&String::from("abc")[0..1]); - ???(" hello there ".trim()); - ???("Happy Monday!".to_string().replace("Mon", "Tues")); - ???("mY sHiFt KeY iS sTiCkY".to_lowercase()); + string_slice("blue"); + string("red".to_string()); + string(String::from("hi")); + string("rust is fun!".to_owned()); + string("nice weather".into()); + string(format!("Interpolation {}", "Station")); + string_slice(&String::from("abc")[0..1]); + string_slice(" hello there ".trim()); + string("Happy Monday!".to_string().replace("Mon", "Tues")); + string("mY sHiFt KeY iS sTiCkY".to_lowercase()); } diff --git a/exercises/10_modules/modules1.rs b/exercises/10_modules/modules1.rs index 9eb5a48b..50b60bcd 100644 --- a/exercises/10_modules/modules1.rs +++ b/exercises/10_modules/modules1.rs @@ -3,7 +3,6 @@ // 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! @@ -11,7 +10,7 @@ mod sausage_factory { String::from("Ginger") } - fn make_sausage() { + pub fn make_sausage() { get_secret_recipe(); println!("sausage!"); } diff --git a/exercises/10_modules/modules2.rs b/exercises/10_modules/modules2.rs index 04154543..47a5bbdc 100644 --- a/exercises/10_modules/modules2.rs +++ b/exercises/10_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/10_modules/modules3.rs b/exercises/10_modules/modules3.rs index f2bb0503..d216c7b5 100644 --- a/exercises/10_modules/modules3.rs +++ b/exercises/10_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/11_hashmaps/hashmaps1.rs b/exercises/11_hashmaps/hashmaps1.rs index 80829eaa..eb7b9823 100644 --- a/exercises/11_hashmaps/hashmaps1.rs +++ b/exercises/11_hashmaps/hashmaps1.rs @@ -11,18 +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); // TODO: Put more fruits in your basket here. - + basket.insert(String::from("apple"), 2); + basket.insert(String::from("gum-gum"), 2); basket } diff --git a/exercises/11_hashmaps/hashmaps2.rs b/exercises/11_hashmaps/hashmaps2.rs index a5925690..3018dfc7 100644 --- a/exercises/11_hashmaps/hashmaps2.rs +++ b/exercises/11_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,9 @@ 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! + if !basket.contains_key(&fruit) { + basket.insert(fruit, 2); + } } } diff --git a/exercises/11_hashmaps/hashmaps3.rs b/exercises/11_hashmaps/hashmaps3.rs index 36544ee3..0f6f9d39 100644 --- a/exercises/11_hashmaps/hashmaps3.rs +++ b/exercises/11_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,12 @@ fn build_scores_table(results: String) -> HashMap { // will be the number of goals conceded by team_2, and similarly // goals scored by team_2 will be the number of goals conceded by // team_1. + scores.entry(team_1_name) + .and_modify(|team| {team.goals_scored = team.goals_scored + team_1_score; team.goals_conceded= team.goals_conceded + team_2_score;}) + .or_insert(Team {goals_scored: team_1_score, goals_conceded: team_2_score}); + scores.entry(team_2_name) + .and_modify(|team| {team.goals_scored= team.goals_scored + team_2_score; team.goals_conceded= team.goals_conceded + team_1_score;}) + .or_insert(Team {goals_scored: team_2_score, goals_conceded: team_1_score}); } scores } diff --git a/exercises/12_options/options1.rs b/exercises/12_options/options1.rs index e131b48b..1ebca1e9 100644 --- a/exercises/12_options/options1.rs +++ b/exercises/12_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,12 @@ 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) + } else if time_of_day > 21 && time_of_day < 24 { + return Some(0) + } + return 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/12_options/options2.rs b/exercises/12_options/options2.rs index 4d998e7d..80c5f45f 100644 --- a/exercises/12_options/options2.rs +++ b/exercises/12_options/options2.rs @@ -3,7 +3,6 @@ // Execute `rustlings hint options2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE #[cfg(test)] mod tests { @@ -13,7 +12,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) = optional_target { assert_eq!(word, target); } } @@ -32,7 +31,7 @@ mod tests { // 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() { + while let Some(Some(integer)) = optional_integers.pop() { assert_eq!(integer, cursor); cursor -= 1; } diff --git a/exercises/12_options/options3.rs b/exercises/12_options/options3.rs index 23c15eab..6edce3ff 100644 --- a/exercises/12_options/options3.rs +++ b/exercises/12_options/options3.rs @@ -3,7 +3,6 @@ // Execute `rustlings hint options3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE struct Point { x: i32, @@ -13,7 +12,7 @@ struct Point { fn main() { let y: Option = Some(Point { x: 100, y: 200 }); - match y { + match &y { Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), _ => panic!("no match!"), } diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 29925caf..1c7734e5 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,10 +31,15 @@ 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() { + output.push(match command { + Command::Uppercase => string.to_uppercase().to_string(), + Command::Trim => string.trim().to_string(), + Command::Append(s) => string.to_string() + &"bar".repeat(*s) + }); // TODO: Complete the function body. You can do it! } output @@ -45,7 +49,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]