2023-05-29 20:39:08 +03:00
|
|
|
// We're collecting different fruits to bake a delicious fruit cake. For this,
|
|
|
|
// we have a basket, which we'll represent in the form of a hash map. The key
|
|
|
|
// represents the name of each fruit we collect and the value represents how
|
|
|
|
// many of that particular fruit we have collected. Three types of fruits -
|
|
|
|
// Apple (4), Mango (2) and Lychee (5) are already in the basket hash map. You
|
|
|
|
// must add fruit to the basket so that there is at least one of each kind and
|
|
|
|
// more than 11 in total - we have a lot of mouths to feed. You are not allowed
|
|
|
|
// to insert any more of these fruits!
|
2020-10-26 15:08:48 +03:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2023-11-22 01:02:26 +03:00
|
|
|
#[derive(Hash, PartialEq, Eq, Debug)]
|
2020-10-26 15:08:48 +03:00
|
|
|
enum Fruit {
|
|
|
|
Apple,
|
|
|
|
Banana,
|
|
|
|
Mango,
|
2021-04-10 00:08:02 +03:00
|
|
|
Lychee,
|
2020-10-26 15:08:48 +03:00
|
|
|
Pineapple,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
2024-06-24 17:50:03 +03:00
|
|
|
let fruit_kinds = [
|
2020-10-26 15:08:48 +03:00
|
|
|
Fruit::Apple,
|
|
|
|
Fruit::Banana,
|
|
|
|
Fruit::Mango,
|
2021-04-10 00:08:02 +03:00
|
|
|
Fruit::Lychee,
|
2020-10-26 15:08:48 +03:00
|
|
|
Fruit::Pineapple,
|
|
|
|
];
|
|
|
|
|
|
|
|
for fruit in fruit_kinds {
|
2023-05-29 20:39:08 +03:00
|
|
|
// 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!
|
2020-10-26 15:08:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 23:46:21 +03:00
|
|
|
fn main() {
|
|
|
|
// You can optionally experiment here.
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:08:48 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2023-03-20 14:04:47 +03:00
|
|
|
// Don't modify this function!
|
2020-10-26 15:08:48 +03:00
|
|
|
fn get_fruit_basket() -> HashMap<Fruit, u32> {
|
2024-06-24 17:50:03 +03:00
|
|
|
let content = [(Fruit::Apple, 4), (Fruit::Mango, 2), (Fruit::Lychee, 5)];
|
|
|
|
HashMap::from_iter(content)
|
2020-10-26 15:08:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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);
|
2021-04-10 00:08:02 +03:00
|
|
|
assert_eq!(*basket.get(&Fruit::Lychee).unwrap(), 5);
|
2020-10-26 15:08:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn at_least_five_types_of_fruits() {
|
|
|
|
let mut basket = get_fruit_basket();
|
|
|
|
fruit_basket(&mut basket);
|
|
|
|
let count_fruit_kinds = basket.len();
|
2021-04-20 12:15:49 +03:00
|
|
|
assert!(count_fruit_kinds >= 5);
|
2020-10-26 15:08:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn greater_than_eleven_fruits() {
|
|
|
|
let mut basket = get_fruit_basket();
|
|
|
|
fruit_basket(&mut basket);
|
2021-03-18 20:45:01 +03:00
|
|
|
let count = basket.values().sum::<u32>();
|
2020-10-26 15:08:48 +03:00
|
|
|
assert!(count > 11);
|
|
|
|
}
|
2024-04-17 23:46:21 +03:00
|
|
|
|
2023-06-08 16:49:07 +03:00
|
|
|
#[test]
|
|
|
|
fn all_fruit_types_in_basket() {
|
2024-06-24 17:50:03 +03:00
|
|
|
let fruit_kinds = [
|
2024-03-28 06:37:19 +03:00
|
|
|
Fruit::Apple,
|
|
|
|
Fruit::Banana,
|
|
|
|
Fruit::Mango,
|
|
|
|
Fruit::Lychee,
|
|
|
|
Fruit::Pineapple,
|
|
|
|
];
|
|
|
|
|
2023-06-08 16:49:07 +03:00
|
|
|
let mut basket = get_fruit_basket();
|
|
|
|
fruit_basket(&mut basket);
|
2024-06-24 17:50:03 +03:00
|
|
|
|
2024-03-28 06:37:19 +03:00
|
|
|
for fruit_kind in fruit_kinds {
|
2024-06-24 17:50:03 +03:00
|
|
|
let Some(amount) = basket.get(&fruit_kind) else {
|
|
|
|
panic!("Fruit kind {fruit_kind:?} was not found in basket");
|
|
|
|
};
|
|
|
|
assert!(*amount > 0);
|
2023-06-08 16:49:07 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-26 15:08:48 +03:00
|
|
|
}
|