Skip to content

Commit 098054a

Browse files
committed
Iterators F
1 parent b21f9b6 commit 098054a

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

exercises/iterators/iterators4.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,8 @@
33
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
86
pub fn factorial(num: u64) -> u64 {
9-
// Complete this function to return the factorial of num
10-
// Do not use:
11-
// - return
12-
// Try not to use:
13-
// - imperative style loops (for, while)
14-
// - additional variables
15-
// For an extra challenge, don't use:
16-
// - recursion
17-
// Execute `rustlings hint iterators4` for hints.
7+
(1..=num).product()
188
}
199

2010
#[cfg(test)]
@@ -30,6 +20,7 @@ mod tests {
3020
fn factorial_of_1() {
3121
assert_eq!(1, factorial(1));
3222
}
23+
3324
#[test]
3425
fn factorial_of_2() {
3526
assert_eq!(2, factorial(2));

exercises/iterators/iterators5.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
1212
// hint.
1313

14-
// I AM NOT DONE
15-
1614
use std::collections::HashMap;
1715

1816
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -33,9 +31,7 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
3331
}
3432

3533
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
36-
// map is a hashmap with String keys and Progress values.
37-
// map = { "variables1": Complete, "from_str": None, ... }
38-
todo!();
34+
map.values().filter(|&&v| v == value).count()
3935
}
4036

4137
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
@@ -51,10 +47,11 @@ fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progres
5147
}
5248

5349
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
54-
// collection is a slice of hashmaps.
55-
// collection = [{ "variables1": Complete, "from_str": None, ... },
56-
// { "variables2": Complete, ... }, ... ]
57-
todo!();
50+
collection
51+
.iter()
52+
.flat_map(|map| map.values())
53+
.filter(|&&v| v == value)
54+
.count()
5855
}
5956

6057
#[cfg(test)]

0 commit comments

Comments
 (0)