Skip to content

Commit

Permalink
added a codewars solution that accepts '&[Either<i32, String>]' type …
Browse files Browse the repository at this point in the history
…and returning i32
  • Loading branch information
cosmir17 committed Jun 4, 2024
1 parent 26415d3 commit ca8edf0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions codewars_kata_training/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
27 changes: 27 additions & 0 deletions codewars_kata_training/examples/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
#[cfg(test)]
mod div_con_tests {
use either::Either;
fn div_con(arr: &[Either<i32, String>]) -> i32 {
arr.iter().fold(0, |acc, item| {
match item {
Either::Left(num) => acc + num,
Either::Right(s) => acc - s.parse::<i32>().unwrap_or(0),
}
})
}

fn dotest(arr: &[Either<i32, String>], 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)
Expand Down

0 comments on commit ca8edf0

Please sign in to comment.