diff --git a/codewars_kata_training/Cargo.toml b/codewars_kata_training/Cargo.toml index 31a09b0..931061e 100644 --- a/codewars_kata_training/Cargo.toml +++ b/codewars_kata_training/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +either = "1.12.0" diff --git a/codewars_kata_training/examples/main.rs b/codewars_kata_training/examples/main.rs index 30f250c..19965a4 100644 --- a/codewars_kata_training/examples/main.rs +++ b/codewars_kata_training/examples/main.rs @@ -1,3 +1,30 @@ +#[cfg(test)] +mod div_con_tests { + use either::Either; + fn div_con(arr: &[Either]) -> i32 { + arr.iter().fold(0, |acc, item| { + match item { + Either::Left(num) => acc + num, + Either::Right(s) => acc - s.parse::().unwrap_or(0), + } + }) + } + + fn dotest(arr: &[Either], expected: i32) { + let actual = div_con(arr); + assert!(actual == expected, "With arr = {arr:?}\nExpected {expected} but got {actual}") + } + + #[test] + fn fixed_tests() { + dotest(&[either::Left(9), either::Left(3), either::Right("7".to_string()), either::Right("3".to_string())], 2); + dotest(&[Either::Right("5".to_string()), Either::Right("0".to_string().to_string()), Either::Left(9), Either::Left(3), Either::Left(2), Either::Left(1), Either::Right("9".to_string()), Either::Left(6), Either::Left(7)], 14); + dotest(&[Either::Right("3".to_string()), Either::Left(6), Either::Left(6), Either::Left(0), Either::Right("5".to_string()), Either::Left(8), Either::Left(5), Either::Right("6".to_string()), Either::Left(2), Either::Right("0".to_string())], 13); + dotest(&[Either::Right("1".to_string()), Either::Right("5".to_string()), Either::Right("8".to_string()), Either::Left(8), Either::Left(9), Either::Left(9), Either::Left(2), Either::Right("3".to_string())], 11); + dotest(&[Either::Left(8), Either::Left(0), Either::Left(0), Either::Left(8), Either::Left(5), Either::Left(7), Either::Left(2), Either::Left(3), Either::Left(7), Either::Left(8), Either::Left(6), Either::Left(7)], 61); + } +} + fn solution(word: &str, ending: &str) -> bool { word.ends_with(ending)