diff --git a/exercises/iterators/iterators3.rs b/exercises/iterators/iterators3.rs index c97a625..74c9d5a 100644 --- a/exercises/iterators/iterators3.rs +++ b/exercises/iterators/iterators3.rs @@ -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,29 @@ 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 { - todo!(); + if a.checked_div(b).is_none() { + Err(DivisionError::DivideByZero) + } else if a.checked_rem(b) != Some(0) { + Err(DivisionError::NotDivisible(NotDivisibleError {dividend: a, divisor: b})) + } else { + 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, DivisionError> { let numbers = vec![27, 297, 38502, 81]; - let division_results = numbers.into_iter().map(|n| divide(n, 27)); + let division_results = numbers.into_iter().map(|n| divide(n, 27).unwrap()); + Ok(Vec::from_iter(division_results)) } // 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> { let numbers = vec![27, 297, 38502, 81]; let division_results = numbers.into_iter().map(|n| divide(n, 27)); + Vec::from_iter(division_results) } #[cfg(test)] diff --git a/exercises/iterators/iterators4.rs b/exercises/iterators/iterators4.rs index a02470e..126a063 100644 --- a/exercises/iterators/iterators4.rs +++ b/exercises/iterators/iterators4.rs @@ -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,24 @@ 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, |a, b| a * b) + // Explicación con ejemplo factorial(4): + // (1..num + 1) es la iteración (1, 2, 3, 4) porque: + // * La iteración tiene un límite de elementos igual a num + 1, en este caso 5: + // * (0, 1, 2, 3, 4) => 5 elementos + // * Sin embargo, solo toma a partir del elemento 1: + // * (1, 2, 3, 4) + // * Una manera más elegante es: + // (1..=num), demostración: + // assert_eq!((1..num + 1), (1..=num)); + // De ahí el fold realiza la siguiente multiplicación: + // ((((1 * 1) * 2) * 3) * 4) + // * El primer argumento de fold es 1 y este corresponde al primer 1 en (1 * 1) + // * La variable 'a' es el resultado acumulativo de a * b + // * La variable 'b' es cada uno de los elementos del iterador (1, 2, 3, 4) + // Una opción más sintética hubiera sido: + // (1..=num).product() + // Sin embargo, el hint pidió revisar los métodos 'fold' y 'rfold'… } #[cfg(test)] diff --git a/temp_265943_ThreadId1 b/temp_265943_ThreadId1 new file mode 100644 index 0000000..2d9e571 Binary files /dev/null and b/temp_265943_ThreadId1 differ