feat: complete iterators

This commit is contained in:
yedidya rashi 2023-04-10 14:58:40 +03:00
parent 04f8d9dd13
commit 74b9efeaff
4 changed files with 31 additions and 19 deletions

View File

@ -3,8 +3,6 @@
// can offer. Follow the steps to complete the exercise.
// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
// Step 1.
// Complete the `capitalize_first` function.
// "hello" -> "Hello"
@ -29,10 +27,7 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
// Return a single string.
// ["hello", " ", "world"] -> "Hello World"
pub fn capitalize_words_string(words: &[&str]) -> String {
words
.iter()
.map(|s| capitalize_first(s))
.collect()
words.iter().map(|s| capitalize_first(s)).collect()
}
#[cfg(test)]

View File

@ -6,8 +6,6 @@
// list_of_results functions.
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#[derive(Debug, PartialEq, Eq)]
pub enum DivisionError {
NotDivisible(NotDivisibleError),
@ -23,21 +21,31 @@ pub struct NotDivisibleError {
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
// Otherwise, return a suitable error.
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
todo!();
if b == 0 {
return Err(DivisionError::DivideByZero);
}
if a % b != 0 {
return Err(DivisionError::NotDivisible(NotDivisibleError {
dividend: a,
divisor: b,
}));
}
return Ok(a / b);
}
// Complete the function and return a value of the correct type so the test passes.
// Desired output: Ok([1, 11, 1426, 3])
fn result_with_list() -> () {
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
return numbers.into_iter().map(|n| divide(n, 27)).collect();
}
// Complete the function and return a value of the correct type so the test passes.
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
fn list_of_results() -> () {
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
numbers.into_iter().map(|n| divide(n, 27)).collect()
}
#[cfg(test)]

View File

@ -1,8 +1,6 @@
// iterators4.rs
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
pub fn factorial(num: u64) -> u64 {
// Complete this function to return the factorial of num
// Do not use:
@ -13,6 +11,9 @@ pub fn factorial(num: u64) -> u64 {
// For an extra challenge, don't use:
// - recursion
// Execute `rustlings hint iterators4` for hints.
// return (1..=num).product();
return (1..=num).fold(1, |acc, curr| curr * acc);
}
#[cfg(test)]

View File

@ -10,8 +10,6 @@
//
// Make the code compile and the tests pass.
// I AM NOT DONE
use std::collections::HashMap;
#[derive(Clone, Copy, PartialEq, Eq)]
@ -34,7 +32,14 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
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, ... }
todo!();
map.iter().fold(0, |acc, (_, _value)| {
if _value == &value {
return acc + 1;
}
return acc;
})
// map.iter().filter(|(_, v)| *v == &value).count()
}
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
@ -53,7 +58,10 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
// collection is a slice of hashmaps.
// collection = [{ "variables1": Complete, "from_str": None, ... },
// { "variables2": Complete, ... }, ... ]
todo!();
collection
.iter()
.map(|item| count_iterator(item, value))
.sum()
}
#[cfg(test)]