Skip to content

Commit 59e930c

Browse files
committed
update
1 parent 6ae6c85 commit 59e930c

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

exercises/iterators/iterators3.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
1010
// hint.
1111

12-
// I AM NOT DONE
12+
1313

1414
#[derive(Debug, PartialEq, Eq)]
1515
pub enum DivisionError {
@@ -26,23 +26,31 @@ pub struct NotDivisibleError {
2626
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
2727
// Otherwise, return a suitable error.
2828
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
29-
todo!();
29+
if b == 0 {
30+
Err(DivisionError::DivideByZero)
31+
} else if a % b == 0 {
32+
Ok(a / b)
33+
} else {
34+
Err(DivisionError::NotDivisible(NotDivisibleError { dividend: a, divisor: b }))
35+
}
3036
}
3137

3238
// Complete the function and return a value of the correct type so the test
3339
// passes.
3440
// Desired output: Ok([1, 11, 1426, 3])
35-
fn result_with_list() -> () {
41+
fn result_with_list() -> Result<Vec<i32>,DivisionError> {
3642
let numbers = vec![27, 297, 38502, 81];
3743
let division_results = numbers.into_iter().map(|n| divide(n, 27));
44+
Ok(division_results.map(|x| x.unwrap()).collect())
3845
}
3946

4047
// Complete the function and return a value of the correct type so the test
4148
// passes.
4249
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
43-
fn list_of_results() -> () {
50+
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
4451
let numbers = vec![27, 297, 38502, 81];
4552
let division_results = numbers.into_iter().map(|n| divide(n, 27));
53+
division_results.collect()
4654
}
4755

4856
#[cfg(test)]

exercises/iterators/iterators4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
76

87
pub fn factorial(num: u64) -> u64 {
98
// Complete this function to return the factorial of num
@@ -15,6 +14,7 @@ pub fn factorial(num: u64) -> u64 {
1514
// For an extra challenge, don't use:
1615
// - recursion
1716
// Execute `rustlings hint iterators4` for hints.
17+
(1..=num).fold(1,|acc,x| acc*x)
1818
}
1919

2020
#[cfg(test)]

exercises/iterators/iterators5.rs

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

14-
// I AM NOT DONE
14+
1515

1616
use std::collections::HashMap;
1717

@@ -35,7 +35,8 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
3535
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
3636
// map is a hashmap with String keys and Progress values.
3737
// map = { "variables1": Complete, "from_str": None, ... }
38-
todo!();
38+
39+
map.iter().filter(|x| x.1==&value).count()
3940
}
4041

4142
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
@@ -54,7 +55,7 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
5455
// collection is a slice of hashmaps.
5556
// collection = [{ "variables1": Complete, "from_str": None, ... },
5657
// { "variables2": Complete, ... }, ... ]
57-
todo!();
58+
collection.iter().flat_map(|map| map.values()).filter(|val| val==&&value).count()
5859
}
5960

6061
#[cfg(test)]

0 commit comments

Comments
 (0)