Skip to content

Commit

Permalink
Merge pull request #19 from Joaopmorais/iterators
Browse files Browse the repository at this point in the history
iterator exeercises
  • Loading branch information
Joaopmorais committed Feb 21, 2024
2 parents 9149644 + 0cca76d commit 81c1365
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
14 changes: 6 additions & 8 deletions exercises/18_iterators/iterators1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[test]
fn main() {
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];

let mut my_iterable_fav_fruits = ???; // TODO: Step 1
let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1

assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
}
8 changes: 3 additions & 5 deletions exercises/18_iterators/iterators2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
// 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"
pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars();
match c.next() {
None => String::new(),
Some(first) => ???,
Some(first) => first.to_uppercase().collect::<String>() + c.as_str(),
}
}

Expand All @@ -24,15 +22,15 @@ pub fn capitalize_first(input: &str) -> String {
// Return a vector of strings.
// ["hello", "world"] -> ["Hello", "World"]
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
vec![]
words.to_owned().into_iter().map(capitalize_first).collect()
}

// Step 3.
// Apply the `capitalize_first` function again to a slice of string slices.
// Return a single string.
// ["hello", " ", "world"] -> "Hello World"
pub fn capitalize_words_string(words: &[&str]) -> String {
String::new()
words.to_owned().into_iter().map(capitalize_first).collect::<String>()
}

#[cfg(test)]
Expand Down
17 changes: 13 additions & 4 deletions exercises/18_iterators/iterators3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[derive(Debug, PartialEq, Eq)]
pub enum DivisionError {
Expand All @@ -26,23 +25,33 @@ 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{
Err(DivisionError::DivideByZero)
}else {
if a % b == 0 {
Ok(a/b)
}else {
Err(DivisionError::NotDivisible(NotDivisibleError { dividend: (a), divisor: (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));
division_results.filter(|x| x.is_ok()).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));
division_results.filter(|x| x.is_ok()).collect::<Vec<Result<i32, DivisionError>>>()
}

#[cfg(test)]
Expand Down
3 changes: 1 addition & 2 deletions exercises/18_iterators/iterators4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// 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:
Expand All @@ -15,6 +13,7 @@ pub fn factorial(num: u64) -> u64 {
// For an extra challenge, don't use:
// - recursion
// Execute `rustlings hint iterators4` for hints.
(1..num + 1).fold(1,|acc, x| acc * x)
}

#[cfg(test)]
Expand Down
6 changes: 2 additions & 4 deletions exercises/18_iterators/iterators5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::collections::HashMap;

#[derive(Clone, Copy, PartialEq, Eq)]
Expand All @@ -35,7 +33,7 @@ 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, (_, v)| { if v == &value {acc + 1} else {acc}})
}

fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
Expand All @@ -54,7 +52,7 @@ 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().fold(0, |acc, x| acc + count_iterator(x, value))
}

#[cfg(test)]
Expand Down

0 comments on commit 81c1365

Please sign in to comment.