Ejercicios 74/94
This commit is contained in:
parent
89d303737e
commit
ce466b57cf
|
@ -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<i32, DivisionError> {
|
||||
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<Vec<i32>, 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<Result<i32, DivisionError>> {
|
||||
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)]
|
||||
|
|
|
@ -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)]
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue