resolved iterators

This commit is contained in:
horaoen 2024-01-27 14:53:02 +08:00
parent 3cdc3bdb9e
commit 5a75a09701
2 changed files with 6 additions and 20 deletions

View File

@ -3,18 +3,9 @@
// 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:
// - return
// Try not to use:
// - imperative style loops (for, while)
// - additional variables
// For an extra challenge, don't use:
// - recursion
// Execute `rustlings hint iterators4` for hints.
// (1..=n).fold(1, |acc, x| acc * x)
(1..=num).product()
}
#[cfg(test)]

View File

@ -11,8 +11,6 @@
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
use std::collections::HashMap;
#[derive(Clone, Copy, PartialEq, Eq)]
@ -33,9 +31,7 @@ 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!();
map.values().filter(|val| val == &&value).count()
}
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
@ -51,10 +47,9 @@ fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progres
}
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
// collection is a slice of hashmaps.
// collection = [{ "variables1": Complete, "from_str": None, ... },
// { "variables2": Complete, ... }, ... ]
todo!();
collection.iter().enumerate().fold(0, |acc, (_, map)| {
acc + map.values().filter(|val| val == &&value).count()
})
}
#[cfg(test)]