mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-07 03:09:19 +00:00
resolved iterators
This commit is contained in:
parent
3cdc3bdb9e
commit
5a75a09701
@ -3,18 +3,9 @@
|
|||||||
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// 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
|
// (1..=n).fold(1, |acc, x| acc * x)
|
||||||
// Do not use:
|
(1..=num).product()
|
||||||
// - 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.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -11,8 +11,6 @@
|
|||||||
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
#[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 {
|
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||||
// map is a hashmap with String keys and Progress values.
|
map.values().filter(|val| val == &&value).count()
|
||||||
// map = { "variables1": Complete, "from_str": None, ... }
|
|
||||||
todo!();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
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 {
|
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||||||
// collection is a slice of hashmaps.
|
collection.iter().enumerate().fold(0, |acc, (_, map)| {
|
||||||
// collection = [{ "variables1": Complete, "from_str": None, ... },
|
acc + map.values().filter(|val| val == &&value).count()
|
||||||
// { "variables2": Complete, ... }, ... ]
|
})
|
||||||
todo!();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user