rustlings/exercises/18_iterators/iterators5.rs

154 lines
4.4 KiB
Rust
Raw Normal View History

2024-06-28 10:11:14 -04:00
// Let's define a simple model to track Rustlings' exercise progress. Progress
2021-04-20 18:52:10 -04:00
// 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. Recreate this counting
2024-06-28 10:11:14 -04:00
// functionality using iterators. Try to not use imperative loops (for/while).
2021-02-10 18:03:29 -05:00
use std::collections::HashMap;
#[derive(Clone, Copy, PartialEq, Eq)]
2021-02-10 18:03:29 -05:00
enum Progress {
None,
Some,
Complete,
}
fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
let mut count = 0;
for val in map.values() {
2024-06-28 10:11:14 -04:00
if *val == value {
2021-02-10 18:03:29 -05:00
count += 1;
}
}
count
}
2024-06-28 10:11:14 -04:00
// TODO: Implement the functionality of `count_for` but with an iterator instead
// of a `for` loop.
2021-04-20 18:52:10 -04:00
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
2024-06-28 10:11:14 -04:00
// `map` is a hash map with `String` keys and `Progress` values.
// map = { "variables1": Complete, "from_str": None, … }
2021-02-10 18:03:29 -05:00
}
2021-04-20 18:52:10 -04:00
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
2021-02-10 18:03:29 -05:00
let mut count = 0;
2021-04-20 18:52:10 -04:00
for map in collection {
2021-02-10 18:03:29 -05:00
for val in map.values() {
2024-06-28 10:11:14 -04:00
if *val == value {
2021-02-10 18:03:29 -05:00
count += 1;
}
}
}
count
}
2024-06-28 10:11:14 -04:00
// TODO: Implement the functionality of `count_collection_for` but with an
// iterator instead of a `for` loop.
2021-04-20 18:52:10 -04:00
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
2024-06-28 10:11:14 -04:00
// `collection` is a slice of hash maps.
// collection = [{ "variables1": Complete, "from_str": None, … },
// { "variables2": Complete, … }, … ]
2021-02-10 18:03:29 -05:00
}
fn main() {
// You can optionally experiment here.
}
2021-02-10 18:03:29 -05:00
#[cfg(test)]
mod tests {
use super::*;
2024-06-28 10:11:14 -04:00
fn get_map() -> HashMap<String, Progress> {
use Progress::*;
let mut map = HashMap::new();
map.insert(String::from("variables1"), Complete);
map.insert(String::from("functions1"), Complete);
map.insert(String::from("hashmap1"), Complete);
map.insert(String::from("arc1"), Some);
map.insert(String::from("as_ref_mut"), None);
map.insert(String::from("from_str"), None);
map
}
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
use Progress::*;
let map = get_map();
let mut other = HashMap::new();
other.insert(String::from("variables2"), Complete);
other.insert(String::from("functions2"), Complete);
other.insert(String::from("if1"), Complete);
other.insert(String::from("from_into"), None);
other.insert(String::from("try_from_into"), None);
vec![map, other]
}
2021-02-10 18:03:29 -05:00
#[test]
fn count_complete() {
let map = get_map();
2024-06-28 10:11:14 -04:00
assert_eq!(count_iterator(&map, Progress::Complete), 3);
2021-02-10 18:03:29 -05:00
}
#[test]
fn count_some() {
2021-02-10 18:03:29 -05:00
let map = get_map();
2024-06-28 10:11:14 -04:00
assert_eq!(count_iterator(&map, Progress::Some), 1);
}
#[test]
fn count_none() {
let map = get_map();
2024-06-28 10:11:14 -04:00
assert_eq!(count_iterator(&map, Progress::None), 2);
}
#[test]
2023-05-17 10:02:39 -04:00
fn count_complete_equals_for() {
let map = get_map();
2024-06-28 10:11:14 -04:00
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
for progress_state in progress_states {
assert_eq!(
count_for(&map, progress_state),
2024-06-28 10:11:14 -04:00
count_iterator(&map, progress_state),
);
}
2021-02-10 18:03:29 -05:00
}
#[test]
2021-04-20 18:52:10 -04:00
fn count_collection_complete() {
let collection = get_vec_map();
assert_eq!(
2024-06-28 10:11:14 -04:00
count_collection_iterator(&collection, Progress::Complete),
2021-04-20 18:52:10 -04:00
6,
);
2021-02-10 18:03:29 -05:00
}
#[test]
fn count_collection_some() {
2021-04-20 18:52:10 -04:00
let collection = get_vec_map();
2024-06-28 10:11:14 -04:00
assert_eq!(count_collection_iterator(&collection, Progress::Some), 1);
2021-02-10 18:03:29 -05:00
}
#[test]
fn count_collection_none() {
let collection = get_vec_map();
2024-06-28 10:11:14 -04:00
assert_eq!(count_collection_iterator(&collection, Progress::None), 4);
}
#[test]
fn count_collection_equals_for() {
let collection = get_vec_map();
2024-06-28 10:11:14 -04:00
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
for progress_state in progress_states {
assert_eq!(
count_collection_for(&collection, progress_state),
2024-06-28 10:11:14 -04:00
count_collection_iterator(&collection, progress_state),
);
}
}
2021-02-10 18:03:29 -05:00
}