|
| 1 | +// iterators5.rs |
| 2 | + |
| 3 | +// Rustling progress is modelled using a hash map. The name of the exercise is |
| 4 | +// the key and the progress is the value. Two counting functions were created |
| 5 | +// to count the number of exercises with a given progress. These counting |
| 6 | +// functions use imperative style for loops. Recreate this counting |
| 7 | +// functionality using iterators. |
| 8 | +// Execute `rustlings hint iterators5` for hints. |
| 9 | +// |
| 10 | +// Make the code compile and the tests pass. |
| 11 | + |
| 12 | +// I AM NOT DONE |
| 13 | + |
| 14 | +use std::collections::HashMap; |
| 15 | + |
| 16 | +#[derive(PartialEq, Eq)] |
| 17 | +enum Progress { |
| 18 | + None, |
| 19 | + Some, |
| 20 | + Complete, |
| 21 | +} |
| 22 | + |
| 23 | +fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize { |
| 24 | + let mut count = 0; |
| 25 | + for val in map.values() { |
| 26 | + if val == &value { |
| 27 | + count += 1; |
| 28 | + } |
| 29 | + } |
| 30 | + count |
| 31 | +} |
| 32 | + |
| 33 | +fn count(map: &HashMap<String, Progress>, value: Progress) -> usize { |
| 34 | +} |
| 35 | + |
| 36 | +fn count_stack_for(stack: &[HashMap<String, Progress>], value: Progress) -> usize { |
| 37 | + let mut count = 0; |
| 38 | + for map in stack { |
| 39 | + for val in map.values() { |
| 40 | + if val == &value { |
| 41 | + count += 1; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + count |
| 46 | +} |
| 47 | + |
| 48 | +fn count_stack(stack: &[HashMap<String, Progress>], value: Progress) -> usize { |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod tests { |
| 53 | + use super::*; |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn count_complete() { |
| 57 | + let map = get_map(); |
| 58 | + assert_eq!(3, count(&map, Progress::Complete)); |
| 59 | + } |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn count_equals_for() { |
| 63 | + let map = get_map(); |
| 64 | + assert_eq!( |
| 65 | + count_for(&map, Progress::Complete), |
| 66 | + count(&map, Progress::Complete) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn count_stack_complete() { |
| 72 | + let stack = get_map_stack(); |
| 73 | + assert_eq!(6, count_stack(&stack, Progress::Complete)); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn count_stack_equals_for() { |
| 78 | + let stack = get_map_stack(); |
| 79 | + assert_eq!( |
| 80 | + count_stack_for(&stack, Progress::Complete), |
| 81 | + count_stack(&stack, Progress::Complete) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + fn get_map() -> HashMap<String, Progress> { |
| 86 | + use Progress::*; |
| 87 | + |
| 88 | + let mut map = HashMap::new(); |
| 89 | + map.insert(String::from("variables1"), Complete); |
| 90 | + map.insert(String::from("functions1"), Complete); |
| 91 | + map.insert(String::from("hashmap1"), Complete); |
| 92 | + map.insert(String::from("arc1"), Some); |
| 93 | + map.insert(String::from("as_ref_mut"), None); |
| 94 | + map.insert(String::from("from_str"), None); |
| 95 | + |
| 96 | + map |
| 97 | + } |
| 98 | + |
| 99 | + fn get_map_stack() -> Vec<HashMap<String, Progress>> { |
| 100 | + use Progress::*; |
| 101 | + |
| 102 | + let map = get_map(); |
| 103 | + |
| 104 | + let mut other = HashMap::new(); |
| 105 | + other.insert(String::from("variables2"), Complete); |
| 106 | + other.insert(String::from("functions2"), Complete); |
| 107 | + other.insert(String::from("if1"), Complete); |
| 108 | + other.insert(String::from("from_into"), None); |
| 109 | + other.insert(String::from("try_from_into"), None); |
| 110 | + |
| 111 | + vec![map, other] |
| 112 | + } |
| 113 | +} |
0 commit comments