From 71a17b5616cc235cb0148fbba18eb8c96a180da4 Mon Sep 17 00:00:00 2001 From: Tirth Patel Date: Fri, 12 Apr 2024 13:07:16 -0230 Subject: [PATCH] last iter --- exercises/18_iterators/iterators5.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/exercises/18_iterators/iterators5.rs b/exercises/18_iterators/iterators5.rs index a062ee4c..0a194993 100644 --- a/exercises/18_iterators/iterators5.rs +++ b/exercises/18_iterators/iterators5.rs @@ -11,7 +11,6 @@ // Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE use std::collections::HashMap; @@ -35,7 +34,13 @@ fn count_for(map: &HashMap, value: Progress) -> usize { fn count_iterator(map: &HashMap, value: Progress) -> usize { // map is a hashmap with String keys and Progress values. // map = { "variables1": Complete, "from_str": None, ... } - todo!(); + map.iter().fold(0 as usize, |acc, x| { + if *x.1 == value + { + return acc+1 + } + acc + }) } fn count_collection_for(collection: &[HashMap], value: Progress) -> usize { @@ -54,7 +59,15 @@ fn count_collection_iterator(collection: &[HashMap], value: Pr // collection is a slice of hashmaps. // collection = [{ "variables1": Complete, "from_str": None, ... }, // { "variables2": Complete, ... }, ... ] - todo!(); + collection.iter().fold(0 as usize, |acc, map| { + acc + map.iter().fold(0 as usize, |inner_acc, val| { + if *val.1 == value + { + return inner_acc + 1 + } + inner_acc + }) + }) } #[cfg(test)]