Skip to content

Commit b29ea17

Browse files
committed
feat: Added iterators5.rs exercise.
1 parent ab57c26 commit b29ea17

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// iterators5.rs
2+
3+
// Rustling progress is modelled using a hash map. The name of the exercise is
4+
// the key and the progress is the value. Two counting functions were created
5+
// to count the number of exercises with a given progress. These counting
6+
// functions use imperative style for loops. Recreate this counting
7+
// functionality using iterators.
8+
// Execute `rustlings hint iterators5` for hints.
9+
//
10+
// Make the code compile and the tests pass.
11+
12+
// I AM NOT DONE
13+
14+
use std::collections::HashMap;
15+
16+
#[derive(PartialEq, Eq)]
17+
enum Progress {
18+
None,
19+
Some,
20+
Complete,
21+
}
22+
23+
fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
24+
let mut count = 0;
25+
for val in map.values() {
26+
if val == &value {
27+
count += 1;
28+
}
29+
}
30+
count
31+
}
32+
33+
fn count(map: &HashMap<String, Progress>, value: Progress) -> usize {
34+
}
35+
36+
fn count_stack_for(stack: &[HashMap<String, Progress>], value: Progress) -> usize {
37+
let mut count = 0;
38+
for map in stack {
39+
for val in map.values() {
40+
if val == &value {
41+
count += 1;
42+
}
43+
}
44+
}
45+
count
46+
}
47+
48+
fn count_stack(stack: &[HashMap<String, Progress>], value: Progress) -> usize {
49+
}
50+
51+
#[cfg(test)]
52+
mod tests {
53+
use super::*;
54+
55+
#[test]
56+
fn count_complete() {
57+
let map = get_map();
58+
assert_eq!(3, count(&map, Progress::Complete));
59+
}
60+
61+
#[test]
62+
fn count_equals_for() {
63+
let map = get_map();
64+
assert_eq!(
65+
count_for(&map, Progress::Complete),
66+
count(&map, Progress::Complete)
67+
);
68+
}
69+
70+
#[test]
71+
fn count_stack_complete() {
72+
let stack = get_map_stack();
73+
assert_eq!(6, count_stack(&stack, Progress::Complete));
74+
}
75+
76+
#[test]
77+
fn count_stack_equals_for() {
78+
let stack = get_map_stack();
79+
assert_eq!(
80+
count_stack_for(&stack, Progress::Complete),
81+
count_stack(&stack, Progress::Complete)
82+
);
83+
}
84+
85+
fn get_map() -> HashMap<String, Progress> {
86+
use Progress::*;
87+
88+
let mut map = HashMap::new();
89+
map.insert(String::from("variables1"), Complete);
90+
map.insert(String::from("functions1"), Complete);
91+
map.insert(String::from("hashmap1"), Complete);
92+
map.insert(String::from("arc1"), Some);
93+
map.insert(String::from("as_ref_mut"), None);
94+
map.insert(String::from("from_str"), None);
95+
96+
map
97+
}
98+
99+
fn get_map_stack() -> Vec<HashMap<String, Progress>> {
100+
use Progress::*;
101+
102+
let map = get_map();
103+
104+
let mut other = HashMap::new();
105+
other.insert(String::from("variables2"), Complete);
106+
other.insert(String::from("functions2"), Complete);
107+
other.insert(String::from("if1"), Complete);
108+
other.insert(String::from("from_into"), None);
109+
other.insert(String::from("try_from_into"), None);
110+
111+
vec![map, other]
112+
}
113+
}

info.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,21 @@ a mutable variable. Or, you might write code utilizing recursion
741741
and a match clause. In Rust you can take another functional
742742
approach, computing the factorial elegantly with ranges and iterators."""
743743

744+
[[exercises]]
745+
name = "iterators5"
746+
path = "exercises/standard_library_types/iterators5.rs"
747+
mode = "test"
748+
hint = """
749+
The documentation for the std::iter::Iterator trait contains numerous methods
750+
that would be helpful here.
751+
752+
Return 0 from count_stack to make the code compile in order to test count.
753+
754+
The stack variable in count_stack is a slice of HashMaps. It needs to be
755+
converted into an iterator in order to use the iterator methods.
756+
757+
The fold method can be useful in the count_stack function."""
758+
744759
# THREADS
745760

746761
[[exercises]]

0 commit comments

Comments
 (0)