2024-11-03 10:39:49 +00:00
|
|
|
use std::f32::consts::E;
|
|
|
|
|
2024-10-28 20:46:17 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
enum DivisionError {
|
|
|
|
// Example: 42 / 0
|
|
|
|
DivideByZero,
|
|
|
|
// Only case for `i64`: `i64::MIN / -1` because the result is `i64::MAX + 1`
|
|
|
|
IntegerOverflow,
|
|
|
|
// Example: 5 / 2 = 2.5
|
|
|
|
NotDivisible,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, return a suitable error.
|
|
|
|
fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
|
2024-11-03 10:39:49 +00:00
|
|
|
if b == 0 {
|
|
|
|
return Err(DivisionError::DivideByZero)
|
|
|
|
}
|
|
|
|
match a.checked_rem(b) {
|
|
|
|
None => {
|
|
|
|
Err(DivisionError::IntegerOverflow)
|
|
|
|
}
|
|
|
|
Some(0) => {
|
|
|
|
let division = a.checked_div(b);
|
|
|
|
if let Some(result) = division {
|
|
|
|
Ok(result)
|
|
|
|
} else {
|
|
|
|
Err(DivisionError::IntegerOverflow)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(_) => {
|
|
|
|
Err(DivisionError::NotDivisible)
|
|
|
|
}
|
|
|
|
}
|
2024-10-28 20:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Desired output: `Ok([1, 11, 1426, 3])`
|
2024-11-03 10:39:49 +00:00
|
|
|
fn result_with_list() -> Result<Vec<i64>, DivisionError> {
|
2024-10-28 20:46:17 +00:00
|
|
|
let numbers = [27, 297, 38502, 81];
|
|
|
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
2024-11-03 10:39:49 +00:00
|
|
|
Ok(division_results.map(|i| {
|
|
|
|
i.unwrap()
|
|
|
|
}).collect::<Vec<i64>>())
|
2024-10-28 20:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]`
|
2024-11-03 10:39:49 +00:00
|
|
|
fn list_of_results() -> Vec<Result<i64, DivisionError>>{
|
2024-10-28 20:46:17 +00:00
|
|
|
let numbers = [27, 297, 38502, 81];
|
|
|
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
2024-11-03 10:39:49 +00:00
|
|
|
division_results.collect()
|
2024-10-28 20:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// You can optionally experiment here.
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_success() {
|
|
|
|
assert_eq!(divide(81, 9), Ok(9));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_divide_by_0() {
|
|
|
|
assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_integer_overflow() {
|
|
|
|
assert_eq!(divide(i64::MIN, -1), Err(DivisionError::IntegerOverflow));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_divisible() {
|
|
|
|
assert_eq!(divide(81, 6), Err(DivisionError::NotDivisible));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_divide_0_by_something() {
|
|
|
|
assert_eq!(divide(0, 81), Ok(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_result_with_list() {
|
|
|
|
assert_eq!(result_with_list().unwrap(), [1, 11, 1426, 3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_list_of_results() {
|
|
|
|
assert_eq!(list_of_results(), [Ok(1), Ok(11), Ok(1426), Ok(3)]);
|
|
|
|
}
|
|
|
|
}
|