Skip to content

Commit 633c00c

Browse files
committed
feat: Add HashMap exercises
1 parent 0c12fa3 commit 633c00c

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

exercises/collections/hashmap1.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// hashmap1.rs
2+
// A basket of fruits in the form of a hash map needs to be defined.
3+
// The key represents the name of the fruit and the value represents
4+
// how many of that particular fruit is in the basket. You have to put
5+
// at least three different types of fruits (e.g apple, banana, mango)
6+
// in the basket and the total count of all the fruits should be at
7+
// least five.
8+
//
9+
// Make me compile and pass the tests!
10+
//
11+
// Execute the command `rustlings hint collections3` if you need
12+
// hints.
13+
14+
// I AM NOT DONE
15+
16+
use std::collections::HashMap;
17+
18+
fn fruit_basket() -> HashMap<String, u32> {
19+
let mut basket = // TODO: declare your hash map here.
20+
21+
// Two bananas are already given for you :)
22+
basket.insert(String::from("banana"), 2);
23+
24+
// TODO: Put more fruits in your basket here.
25+
26+
basket
27+
}
28+
29+
#[cfg(test)]
30+
mod tests {
31+
use super::*;
32+
33+
#[test]
34+
fn at_least_three_types_of_fruits() {
35+
let basket = fruit_basket();
36+
assert!(basket.len() >= 3);
37+
}
38+
39+
#[test]
40+
fn at_least_five_fruits() {
41+
let basket = fruit_basket();
42+
assert!(basket
43+
.values()
44+
.sum::<u32>() >= 5);
45+
}
46+
}

exercises/collections/hashmap2.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

info.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,28 @@ Hint 1: `i` is each element from the Vec as they are being iterated.
400400
Hint 2: Check the suggestion from the compiler error ;)
401401
"""
402402

403+
[[exercises]]
404+
name = "collections3"
405+
path = "exercises/collections/hashmap1.rs"
406+
mode = "test"
407+
hint = """
408+
Hint 1: Take a look at the return type of the function to figure out
409+
the type for the `basket`.
410+
411+
Hint 2: Number of fruits should be at least 5. And you have to put
412+
at least three different types of fruits.
413+
"""
414+
415+
[[exercises]]
416+
name = "collections4"
417+
path = "exercises/collections/hashmap2.rs"
418+
mode = "test"
419+
hint = """
420+
Use the `entry()` and `or_insert()` methods of `HashMap` to achieve this.
421+
422+
Learn more at https://doc.rust-lang.org/stable/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value
423+
"""
424+
403425
# MACROS
404426

405427
[[exercises]]

0 commit comments

Comments
 (0)