From 3c4c9c54c98142f114018bbbe4d0d96d63646a5b Mon Sep 17 00:00:00 2001 From: mokou Date: Tue, 12 Jul 2022 15:18:05 +0200 Subject: feat: remove collections to hashmaps --- exercises/hashmaps/hashmaps2.rs | 81 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 exercises/hashmaps/hashmaps2.rs (limited to 'exercises/hashmaps/hashmaps2.rs') diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs new file mode 100644 index 0000000..1afb830 --- /dev/null +++ b/exercises/hashmaps/hashmaps2.rs @@ -0,0 +1,81 @@ +// hashmaps2.rs + +// A basket of fruits in the form of a hash map is given. The key +// represents the name of the fruit and the value represents how many +// of that particular fruit is in the basket. You have to put *MORE +// THAN 11* fruits in the basket. Three types of fruits - Apple (4), +// Mango (2) and Lychee (5) are already given in the basket. You are +// not allowed to insert any more of these fruits! +// +// Make me pass the tests! +// +// Execute the command `rustlings hint hashmaps2` if you need +// hints. + +// I AM NOT DONE + +use std::collections::HashMap; + +#[derive(Hash, PartialEq, Eq)] +enum Fruit { + Apple, + Banana, + Mango, + Lychee, + Pineapple, +} + +fn fruit_basket(basket: &mut HashMap) { + let fruit_kinds = vec![ + Fruit::Apple, + Fruit::Banana, + Fruit::Mango, + Fruit::Lychee, + Fruit::Pineapple, + ]; + + for fruit in fruit_kinds { + // TODO: Put new fruits if not already present. Note that you + // are not allowed to put any type of fruit that's already + // present! + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn get_fruit_basket() -> HashMap { + let mut basket = HashMap::::new(); + basket.insert(Fruit::Apple, 4); + basket.insert(Fruit::Mango, 2); + basket.insert(Fruit::Lychee, 5); + + basket + } + + #[test] + fn test_given_fruits_are_not_modified() { + let mut basket = get_fruit_basket(); + fruit_basket(&mut basket); + assert_eq!(*basket.get(&Fruit::Apple).unwrap(), 4); + assert_eq!(*basket.get(&Fruit::Mango).unwrap(), 2); + assert_eq!(*basket.get(&Fruit::Lychee).unwrap(), 5); + } + + #[test] + fn at_least_five_types_of_fruits() { + let mut basket = get_fruit_basket(); + fruit_basket(&mut basket); + let count_fruit_kinds = basket.len(); + assert!(count_fruit_kinds >= 5); + } + + #[test] + fn greater_than_eleven_fruits() { + let mut basket = get_fruit_basket(); + fruit_basket(&mut basket); + let count = basket.values().sum::(); + assert!(count > 11); + } +} -- cgit v1.2.3 From ab8572e15bf6765471490281ba2a1c8f88275e40 Mon Sep 17 00:00:00 2001 From: mokou Date: Thu, 14 Jul 2022 12:58:28 +0200 Subject: fix(hashmaps): adjust hint comments --- exercises/hashmaps/hashmaps1.rs | 3 +-- exercises/hashmaps/hashmaps2.rs | 3 +-- exercises/hashmaps/hashmaps3.rs | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) (limited to 'exercises/hashmaps/hashmaps2.rs') diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index 26178b0..fd8dd2f 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -8,8 +8,7 @@ // // Make me compile and pass the tests! // -// Execute the command `rustlings hint hashmaps1` if you need -// hints. +// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a hint. // I AM NOT DONE diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index 1afb830..454b3e1 100644 --- a/exercises/hashmaps/hashmaps2.rs +++ b/exercises/hashmaps/hashmaps2.rs @@ -9,8 +9,7 @@ // // Make me pass the tests! // -// Execute the command `rustlings hint hashmaps2` if you need -// hints. +// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint. // I AM NOT DONE diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 60293f2..18dd44c 100644 --- a/exercises/hashmaps/hashmaps3.rs +++ b/exercises/hashmaps/hashmaps3.rs @@ -12,8 +12,7 @@ // Make me pass the tests! -// Execute the command `rustlings hint hashmap3` if you need -// hints. +// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint. // I AM NOT DONE -- cgit v1.2.3