-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Added iterators5.rs exercise. #646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // iterators5.rs | ||
|
|
||
| // 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 | ||
| // number of exercises with a given progress. These counting functions use | ||
| // imperative style for loops. Recreate this counting functionality using | ||
| // iterators. Only the two iterator methods (count_iterator and | ||
| // count_collection_iterator) need to be modified. | ||
| // Execute `rustlings hint | ||
| // iterators5` for hints. | ||
| // | ||
| // Make the code compile and the tests pass. | ||
|
|
||
| // I AM NOT DONE | ||
|
|
||
| use std::collections::HashMap; | ||
|
|
||
| #[derive(PartialEq, Eq)] | ||
| 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 | ||
| } | ||
|
|
||
| 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, ... } | ||
| } | ||
|
|
||
| fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize { | ||
| let mut count = 0; | ||
| for map in collection { | ||
| for val in map.values() { | ||
| if val == &value { | ||
| count += 1; | ||
| } | ||
| } | ||
| } | ||
| count | ||
| } | ||
|
|
||
| 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, ... }, ... ] | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn count_complete() { | ||
| let map = get_map(); | ||
| assert_eq!(3, count_iterator(&map, Progress::Complete)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn count_equals_for() { | ||
| let map = get_map(); | ||
| assert_eq!( | ||
| count_for(&map, Progress::Complete), | ||
| count_iterator(&map, Progress::Complete) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn count_collection_complete() { | ||
| let collection = get_vec_map(); | ||
| assert_eq!( | ||
| 6, | ||
| count_collection_iterator(&collection, Progress::Complete) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn count_collection_equals_for() { | ||
| let collection = get_vec_map(); | ||
| assert_eq!( | ||
| count_collection_for(&collection, Progress::Complete), | ||
| count_collection_iterator(&collection, Progress::Complete) | ||
| ); | ||
| } | ||
|
|
||
| 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] | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, I would leave comments somewhere in this exercise with notes that explain:
_formethods, but rather implement the iterative version in the other methods