mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 21:59:18 +00:00
day9
This commit is contained in:
parent
dd0f259dd1
commit
d24adeb230
@ -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)]
|
||||
|
||||
@ -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<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 = { "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 {
|
||||
@ -53,7 +60,15 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], 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)]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user