Skip to content

Commit

Permalink
feat: solve a few hashmap exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
paulstey committed Sep 6, 2023
1 parent 1e4ce91 commit c00ccfe
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 18 deletions.
7 changes: 5 additions & 2 deletions exercises/hashmaps/hashmaps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +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<String, u32> {
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("orange"), 2);
basket.insert(String::from("pineapple"), 2);
basket.insert(String::from("kiwi"), 2);
basket.insert(String::from("apple"), 2);

// TODO: Put more fruits in your basket here.

Expand Down
6 changes: 5 additions & 1 deletion exercises/hashmaps/hashmaps2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -40,6 +39,11 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// 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!
match fruit {
Fruit::Banana => basket.insert(Fruit::Banana, 5),
Fruit::Pineapple => basket.insert(Fruit::Pineapple, 5),
_ => None
};
}
}

Expand Down
26 changes: 25 additions & 1 deletion exercises/hashmaps/hashmaps3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,6 +38,31 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// 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.
if let Some(team) = scores.get_mut(&team_1_name) {
team.goals_scored += team_1_score;
team.goals_conceded += team_2_score;
} else {
scores.insert(
team_1_name.clone(),
Team {
goals_scored: team_1_score,
goals_conceded: team_2_score,
},
);
}

if let Some(team) = scores.get_mut(&team_2_name) {
team.goals_scored += team_2_score;
team.goals_conceded += team_1_score;
} else {
scores.insert(
team_2_name.clone(),
Team {
goals_scored: team_2_score,
goals_conceded: team_1_score,
},
);
}
}
scores
}
Expand Down
5 changes: 2 additions & 3 deletions exercises/modules/modules2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 1 addition & 2 deletions exercises/modules/modules3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 11 additions & 3 deletions exercises/options/options1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,7 +12,16 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
// 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 <= 10 {
Some(5)
} else if time_of_day <= 23 {
Some(0)
} else {
None
}


}

#[cfg(test)]
Expand All @@ -34,6 +42,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(0));
}
}
14 changes: 8 additions & 6 deletions exercises/quiz2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
//
// No hints this time!

// I AM NOT DONE

pub enum Command {
Uppercase,
Expand All @@ -32,11 +31,14 @@ mod my_module {
use super::Command;

// TODO: Complete the function signature!
pub fn transformer(input: ???) -> ??? {
// TODO: Complete the output declaration!
let mut output: ??? = vec![];
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
let mut output: Vec<String> = 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(n) => output.push(string.to_string() + &"bar".repeat(*n)),
}
}
output
}
Expand All @@ -45,7 +47,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]
Expand Down

0 comments on commit c00ccfe

Please sign in to comment.