|
| 1 | +// hashmap2.rs |
| 2 | + |
| 3 | +// A basket of fruits in the form of a hash map is given. The key |
| 4 | +// represents the name of the fruit and the value represents how many |
| 5 | +// of that particular fruit is in the basket. You have to put *MORE |
| 6 | +// THAN 11* fruits in the basket. Three types of fruits - Apple (4), |
| 7 | +// Mango (2) and Lichi (5) are already given in the basket. You are |
| 8 | +// not allowed to insert any more of these fruits! |
| 9 | +// |
| 10 | +// Make me pass the tests! |
| 11 | +// |
| 12 | +// Execute the command `rustlings hint collections4` if you need |
| 13 | +// hints. |
| 14 | + |
| 15 | +// I AM NOT DONE |
| 16 | + |
| 17 | +use std::collections::HashMap; |
| 18 | + |
| 19 | +#[derive(Hash, PartialEq, Eq)] |
| 20 | +enum Fruit { |
| 21 | + Apple, |
| 22 | + Banana, |
| 23 | + Mango, |
| 24 | + Lichi, |
| 25 | + Pineapple, |
| 26 | +} |
| 27 | + |
| 28 | +fn fruit_basket(basket: &mut HashMap<Fruit, u32>) { |
| 29 | + let fruit_kinds = vec![ |
| 30 | + Fruit::Apple, |
| 31 | + Fruit::Banana, |
| 32 | + Fruit::Mango, |
| 33 | + Fruit::Lichi, |
| 34 | + Fruit::Pineapple, |
| 35 | + ]; |
| 36 | + |
| 37 | + for fruit in fruit_kinds { |
| 38 | + // TODO: Put new fruits if not already present. Note that you |
| 39 | + // are not allowed to put any type of fruit that's already |
| 40 | + // present! |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[cfg(test)] |
| 45 | +mod tests { |
| 46 | + use super::*; |
| 47 | + |
| 48 | + fn get_fruit_basket() -> HashMap<Fruit, u32> { |
| 49 | + let mut basket = HashMap::<Fruit, u32>::new(); |
| 50 | + basket.insert(Fruit::Apple, 4); |
| 51 | + basket.insert(Fruit::Mango, 2); |
| 52 | + basket.insert(Fruit::Lichi, 5); |
| 53 | + |
| 54 | + basket |
| 55 | + } |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn test_given_fruits_are_not_modified() { |
| 59 | + let mut basket = get_fruit_basket(); |
| 60 | + fruit_basket(&mut basket); |
| 61 | + assert_eq!(*basket.get(&Fruit::Apple).unwrap(), 4); |
| 62 | + assert_eq!(*basket.get(&Fruit::Mango).unwrap(), 2); |
| 63 | + assert_eq!(*basket.get(&Fruit::Lichi).unwrap(), 5); |
| 64 | + } |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn at_least_five_types_of_fruits() { |
| 68 | + let mut basket = get_fruit_basket(); |
| 69 | + fruit_basket(&mut basket); |
| 70 | + let count_fruit_kinds = basket.len(); |
| 71 | + assert!(count_fruit_kinds == 5); |
| 72 | + } |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn greater_than_eleven_fruits() { |
| 76 | + let mut basket = get_fruit_basket(); |
| 77 | + fruit_basket(&mut basket); |
| 78 | + let count = basket |
| 79 | + .values() |
| 80 | + .sum::<u32>(); |
| 81 | + assert!(count > 11); |
| 82 | + } |
| 83 | +} |
0 commit comments