From 0f02a9b9af80e8b62709d2c50b0fcdf5f7478ea7 Mon Sep 17 00:00:00 2001 From: seporterfield <107010978+seporterfield@users.noreply.github.com> Date: Sun, 1 Jan 2023 02:00:36 +0100 Subject: moved iterator exercises --- exercises/iterators/iterators3.rs | 85 ++++++++++++++++++++++++++ exercises/standard_library_types/iterators3.rs | 85 -------------------------- 2 files changed, 85 insertions(+), 85 deletions(-) create mode 100644 exercises/iterators/iterators3.rs delete mode 100644 exercises/standard_library_types/iterators3.rs (limited to 'exercises') diff --git a/exercises/iterators/iterators3.rs b/exercises/iterators/iterators3.rs new file mode 100644 index 0000000..c97a625 --- /dev/null +++ b/exercises/iterators/iterators3.rs @@ -0,0 +1,85 @@ +// iterators3.rs +// This is a bigger exercise than most of the others! You can do it! +// Here is your mission, should you choose to accept it: +// 1. Complete the divide function to get the first four tests to pass. +// 2. Get the remaining tests to pass by completing the result_with_list and +// 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), + DivideByZero, +} + +#[derive(Debug, PartialEq, Eq)] +pub struct NotDivisibleError { + dividend: i32, + divisor: i32, +} + +// 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!(); +} + +// 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() -> () { + let numbers = vec![27, 297, 38502, 81]; + let division_results = numbers.into_iter().map(|n| divide(n, 27)); +} + +// 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() -> () { + let numbers = vec![27, 297, 38502, 81]; + let division_results = numbers.into_iter().map(|n| divide(n, 27)); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_success() { + assert_eq!(divide(81, 9), Ok(9)); + } + + #[test] + fn test_not_divisible() { + assert_eq!( + divide(81, 6), + Err(DivisionError::NotDivisible(NotDivisibleError { + dividend: 81, + divisor: 6 + })) + ); + } + + #[test] + fn test_divide_by_0() { + assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero)); + } + + #[test] + fn test_divide_0_by_something() { + assert_eq!(divide(0, 81), Ok(0)); + } + + #[test] + fn test_result_with_list() { + assert_eq!(format!("{:?}", result_with_list()), "Ok([1, 11, 1426, 3])"); + } + + #[test] + fn test_list_of_results() { + assert_eq!( + format!("{:?}", list_of_results()), + "[Ok(1), Ok(11), Ok(1426), Ok(3)]" + ); + } +} diff --git a/exercises/standard_library_types/iterators3.rs b/exercises/standard_library_types/iterators3.rs deleted file mode 100644 index c97a625..0000000 --- a/exercises/standard_library_types/iterators3.rs +++ /dev/null @@ -1,85 +0,0 @@ -// iterators3.rs -// This is a bigger exercise than most of the others! You can do it! -// Here is your mission, should you choose to accept it: -// 1. Complete the divide function to get the first four tests to pass. -// 2. Get the remaining tests to pass by completing the result_with_list and -// 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), - DivideByZero, -} - -#[derive(Debug, PartialEq, Eq)] -pub struct NotDivisibleError { - dividend: i32, - divisor: i32, -} - -// 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!(); -} - -// 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() -> () { - let numbers = vec![27, 297, 38502, 81]; - let division_results = numbers.into_iter().map(|n| divide(n, 27)); -} - -// 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() -> () { - let numbers = vec![27, 297, 38502, 81]; - let division_results = numbers.into_iter().map(|n| divide(n, 27)); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_success() { - assert_eq!(divide(81, 9), Ok(9)); - } - - #[test] - fn test_not_divisible() { - assert_eq!( - divide(81, 6), - Err(DivisionError::NotDivisible(NotDivisibleError { - dividend: 81, - divisor: 6 - })) - ); - } - - #[test] - fn test_divide_by_0() { - assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero)); - } - - #[test] - fn test_divide_0_by_something() { - assert_eq!(divide(0, 81), Ok(0)); - } - - #[test] - fn test_result_with_list() { - assert_eq!(format!("{:?}", result_with_list()), "Ok([1, 11, 1426, 3])"); - } - - #[test] - fn test_list_of_results() { - assert_eq!( - format!("{:?}", list_of_results()), - "[Ok(1), Ok(11), Ok(1426), Ok(3)]" - ); - } -} -- cgit v1.2.3