diff --git a/exercises/standard_library_types/iterators4.rs b/exercises/standard_library_types/iterators4.rs index a02470ec..27b7ed51 100644 --- a/exercises/standard_library_types/iterators4.rs +++ b/exercises/standard_library_types/iterators4.rs @@ -1,8 +1,6 @@ // iterators4.rs // Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - pub fn factorial(num: u64) -> u64 { // Complete this function to return the factorial of num // Do not use: @@ -13,6 +11,12 @@ pub fn factorial(num: u64) -> u64 { // For an extra challenge, don't use: // - recursion // Execute `rustlings hint iterators4` for hints. + // 求阶乘 f(4) = 4*3*2*1 + // if num <= 1 { + // return 1; + // } + // num * factorial(num - 1) + (1..=num).fold(1, |acc, x| acc * x) } #[cfg(test)] diff --git a/exercises/standard_library_types/iterators5.rs b/exercises/standard_library_types/iterators5.rs index 0593d123..e2897e27 100644 --- a/exercises/standard_library_types/iterators5.rs +++ b/exercises/standard_library_types/iterators5.rs @@ -1,10 +1,13 @@ // iterators5.rs // Let's define a simple model to track Rustlings exercise progress. Progress -// will be modelled using a hash map. The name of the exercise is the key and -// the progress is the value. Two counting functions were created to count the -// number of exercises with a given progress. These counting functions use -// imperative style for loops. Recreate this counting functionality using -// iterators. Only the two iterator methods (count_iterator and +// will be modelled using a hash map. +// The name of the exercise is the key and +// the progress is the value. map(name,progress) +// Two counting functions were created to count the +// number of exercises with a given progress. (统计给定的过程的号码) +// These counting functions use imperative style for loops. 使用了必要的循环 +// Recreate this counting functionality using iterators. 使用迭代器重新创建一个 +// Only the two iterator methods (count_iterator and // count_collection_iterator) need to be modified. // Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a hint. // @@ -34,7 +37,11 @@ fn count_for(map: &HashMap, value: Progress) -> usize { fn count_iterator(map: &HashMap, value: Progress) -> usize { // map is a hashmap with String keys and Progress values. // map = { "variables1": Complete, "from_str": None, ... } - todo!(); + // todo!(); + map.values() + .into_iter() + .filter(|x| **x == value) + .fold(0, |acc, _| acc + 1) } fn count_collection_for(collection: &[HashMap], value: Progress) -> usize { @@ -53,7 +60,15 @@ fn count_collection_iterator(collection: &[HashMap], value: Pr // collection is a slice of hashmaps. // collection = [{ "variables1": Complete, "from_str": None, ... }, // { "variables2": Complete, ... }, ... ] - todo!(); + collection + .iter() + .map(|x| { + x.values() + .into_iter() + .filter(|v| **v == value) + .fold(0, |acc, _| acc + 1) + }) + .sum() } #[cfg(test)]