2021-02-10 23:03:29 +00:00
|
|
|
// iterators5.rs
|
2023-05-29 18:39:08 +01:00
|
|
|
//
|
2021-04-20 23:52:10 +01:00
|
|
|
// 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
|
2023-02-21 14:45:59 +00:00
|
|
|
// number of exercises with a given progress. Recreate this counting
|
|
|
|
// functionality using iterators. Try not to use imperative loops (for, while).
|
|
|
|
// Only the two iterator methods (count_iterator and count_collection_iterator)
|
|
|
|
// need to be modified.
|
2023-05-29 18:39:08 +01:00
|
|
|
//
|
|
|
|
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
|
|
|
|
// hint.
|
2021-02-10 23:03:29 +00:00
|
|
|
|
|
|
|
// I AM NOT DONE
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2021-06-06 23:36:44 +01:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
2021-02-10 23:03:29 +00: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() {
|
|
|
|
if val == &value {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count
|
|
|
|
}
|
|
|
|
|
2021-04-20 23:52:10 +01:00
|
|
|
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, ... }
|
2022-07-14 17:29:09 +01:00
|
|
|
todo!();
|
2021-02-10 23:03:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 23:52:10 +01:00
|
|
|
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
2021-02-10 23:03:29 +00:00
|
|
|
let mut count = 0;
|
2021-04-20 23:52:10 +01:00
|
|
|
for map in collection {
|
2021-02-10 23:03:29 +00:00
|
|
|
for val in map.values() {
|
|
|
|
if val == &value {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count
|
|
|
|
}
|
|
|
|
|
2021-04-20 23:52:10 +01:00
|
|
|
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, ... }, ... ]
|
2022-07-14 17:29:09 +01:00
|
|
|
todo!();
|
2021-02-10 23:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn count_complete() {
|
|
|
|
let map = get_map();
|
2021-04-20 23:52:10 +01:00
|
|
|
assert_eq!(3, count_iterator(&map, Progress::Complete));
|
2021-02-10 23:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-04-15 17:19:16 +01:00
|
|
|
fn count_some() {
|
2021-02-10 23:03:29 +00:00
|
|
|
let map = get_map();
|
2023-04-15 17:19:16 +01:00
|
|
|
assert_eq!(1, count_iterator(&map, Progress::Some));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn count_none() {
|
|
|
|
let map = get_map();
|
|
|
|
assert_eq!(2, count_iterator(&map, Progress::None));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-05-17 15:02:39 +01:00
|
|
|
fn count_complete_equals_for() {
|
2023-04-15 17:19:16 +01:00
|
|
|
let map = get_map();
|
2023-05-23 08:00:55 +01:00
|
|
|
let progress_states = vec![Progress::Complete, Progress::Some, Progress::None];
|
|
|
|
for progress_state in progress_states {
|
2023-04-15 17:19:16 +01:00
|
|
|
assert_eq!(
|
2023-05-23 08:00:55 +01:00
|
|
|
count_for(&map, progress_state),
|
|
|
|
count_iterator(&map, progress_state)
|
2023-04-15 17:19:16 +01:00
|
|
|
);
|
|
|
|
}
|
2021-02-10 23:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-04-20 23:52:10 +01:00
|
|
|
fn count_collection_complete() {
|
|
|
|
let collection = get_vec_map();
|
|
|
|
assert_eq!(
|
|
|
|
6,
|
|
|
|
count_collection_iterator(&collection, Progress::Complete)
|
|
|
|
);
|
2021-02-10 23:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-04-15 17:19:16 +01:00
|
|
|
fn count_collection_some() {
|
2021-04-20 23:52:10 +01:00
|
|
|
let collection = get_vec_map();
|
2023-05-17 15:02:39 +01:00
|
|
|
assert_eq!(1, count_collection_iterator(&collection, Progress::Some));
|
2021-02-10 23:03:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 17:19:16 +01:00
|
|
|
#[test]
|
|
|
|
fn count_collection_none() {
|
|
|
|
let collection = get_vec_map();
|
2023-05-17 15:02:39 +01:00
|
|
|
assert_eq!(4, count_collection_iterator(&collection, Progress::None));
|
2023-04-15 17:19:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn count_collection_equals_for() {
|
2023-05-23 08:00:55 +01:00
|
|
|
let progress_states = vec![Progress::Complete, Progress::Some, Progress::None];
|
2023-04-15 17:19:16 +01:00
|
|
|
let collection = get_vec_map();
|
|
|
|
|
2023-05-23 08:00:55 +01:00
|
|
|
for progress_state in progress_states {
|
2023-04-15 17:19:16 +01:00
|
|
|
assert_eq!(
|
2023-05-23 08:00:55 +01:00
|
|
|
count_collection_for(&collection, progress_state),
|
|
|
|
count_collection_iterator(&collection, progress_state)
|
2023-04-15 17:19:16 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 23:03:29 +00: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
|
|
|
|
}
|
|
|
|
|
2021-04-20 23:52:10 +01:00
|
|
|
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
|
2021-02-10 23:03:29 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|