This commit is contained in:
matytan 2022-11-24 20:58:38 +08:00
parent dd0f259dd1
commit d24adeb230
2 changed files with 28 additions and 9 deletions

View File

@ -1,8 +1,6 @@
// iterators4.rs // iterators4.rs
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
pub fn factorial(num: u64) -> u64 { pub fn factorial(num: u64) -> u64 {
// Complete this function to return the factorial of num // Complete this function to return the factorial of num
// Do not use: // Do not use:
@ -13,6 +11,12 @@ pub fn factorial(num: u64) -> u64 {
// For an extra challenge, don't use: // For an extra challenge, don't use:
// - recursion // - recursion
// Execute `rustlings hint iterators4` for hints. // 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)] #[cfg(test)]

View File

@ -1,10 +1,13 @@
// iterators5.rs // iterators5.rs
// Let's define a simple model to track Rustlings exercise progress. Progress // 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 // will be modelled using a hash map.
// the progress is the value. Two counting functions were created to count the // The name of the exercise is the key and
// number of exercises with a given progress. These counting functions use // the progress is the value. map(name,progress)
// imperative style for loops. Recreate this counting functionality using // Two counting functions were created to count the
// iterators. Only the two iterator methods (count_iterator and // 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. // count_collection_iterator) need to be modified.
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a hint.
// //
@ -34,7 +37,11 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize { fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
// map is a hashmap with String keys and Progress values. // map is a hashmap with String keys and Progress values.
// map = { "variables1": Complete, "from_str": None, ... } // 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<String, Progress>], value: Progress) -> usize { fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
@ -53,7 +60,15 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
// collection is a slice of hashmaps. // collection is a slice of hashmaps.
// collection = [{ "variables1": Complete, "from_str": None, ... }, // collection = [{ "variables1": Complete, "from_str": None, ... },
// { "variables2": Complete, ... }, ... ] // { "variables2": Complete, ... }, ... ]
todo!(); collection
.iter()
.map(|x| {
x.values()
.into_iter()
.filter(|v| **v == value)
.fold(0, |acc, _| acc + 1)
})
.sum()
} }
#[cfg(test)] #[cfg(test)]