diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index fd8dd2f8..d6981a9c 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -10,15 +10,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();// TODO: declare your hash map here. // Two bananas are already given for you :) basket.insert(String::from("banana"), 2); + basket.insert(String::from("apple"), 2); + basket.insert(String::from("mango"), 2); // TODO: Put more fruits in your basket here. diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index 454b3e1d..0cbc86f6 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,9 @@ 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! + if !basket.contains_key(&fruit) { + basket.insert(fruit, 1); + } } } diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 18dd44c9..01e21926 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. @@ -24,6 +22,22 @@ struct Team { goals_scored: u8, goals_conceded: u8, } +impl Team { + fn named(name: String) -> Self { + Self { name: name.to_string(), goals_scored: 0, goals_conceded: 0} + } +} +fn update_score( + scores: &mut HashMap, + name: String, + goals_scored: u8, + goals_conceded: u8 +) + { + let mut team = scores.entry(name.clone()).or_insert(Team::named(name)); + team.goals_scored += goals_scored; + team.goals_conceded += goals_conceded; + } fn build_scores_table(results: String) -> HashMap { // The name of the team is the key and its associated struct is the value. @@ -35,8 +49,12 @@ 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(); + update_score(&mut scores,team_1_name,team_1_score,team_2_score); + update_score(&mut scores,team_2_name,team_2_score,team_1_score); // TODO: Populate the scores table with details extracted from the - // current line. Keep in mind that goals scored by team_1 + // current line. + + //Keep in mind that goals scored by team_1 // 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. diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index d8fa954a..0082b003 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -18,8 +18,6 @@ // - The output element is going to be a Vector of strings. // Execute `rustlings hint quiz2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - pub enum Command { Uppercase, Trim, @@ -30,11 +28,19 @@ 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()), + Command::Trim => output.push(string.trim().to_string()), + Command::Append(usize) => output.push( + format!("{string}{}","bar".repeat(*usize)) + ), + + } } output } @@ -43,7 +49,7 @@ mod my_module { #[cfg(test)] mod tests { // TODO: What do we have to import to have `transformer` in scope? - use ???; + use my_module::transformer; use super::Command; #[test] diff --git a/temp_23683_ThreadId1 b/temp_23683_ThreadId1 new file mode 100755 index 00000000..6d314a41 Binary files /dev/null and b/temp_23683_ThreadId1 differ diff --git a/temp_53856_ThreadId1 b/temp_53856_ThreadId1 new file mode 100755 index 00000000..1d47a8eb Binary files /dev/null and b/temp_53856_ThreadId1 differ