diff options
| -rwxr-xr-x | challenge-330/benjamin-andre/rust/ch-1.rs | 26 | ||||
| -rwxr-xr-x | challenge-330/benjamin-andre/rust/ch-2.rs | 28 |
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-330/benjamin-andre/rust/ch-1.rs b/challenge-330/benjamin-andre/rust/ch-1.rs new file mode 100755 index 0000000000..cb3ebb5d96 --- /dev/null +++ b/challenge-330/benjamin-andre/rust/ch-1.rs @@ -0,0 +1,26 @@ +#!/bin/sh +//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit + +fn clear_digits(s: &str) -> String { + let mut chars: Vec<char> = s.chars().collect(); + let mut i = 0; + while i < chars.len() { + if chars[i].is_ascii_digit() { + chars.remove(i); + if i > 0 { + chars.remove(i - 1); + i -= 1; + } + } else { + i += 1; + } + } + chars.into_iter().collect() +} + +#[test] +fn example() { + assert_eq!(clear_digits("cab12"), "c"); + assert_eq!(clear_digits("xy99"), ""); + assert_eq!(clear_digits("pa1erl"), "perl"); +} diff --git a/challenge-330/benjamin-andre/rust/ch-2.rs b/challenge-330/benjamin-andre/rust/ch-2.rs new file mode 100755 index 0000000000..b11fef1d9b --- /dev/null +++ b/challenge-330/benjamin-andre/rust/ch-2.rs @@ -0,0 +1,28 @@ +#!/bin/sh +//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit + +fn title_capital(s: &str) -> String { + s.split_whitespace() + .map(|word| { + if word.len() <= 2 { + word.to_lowercase() + } else { + let lower = word.to_lowercase(); + let mut chars = lower.chars(); + chars.next().unwrap().to_uppercase().collect::<String>() + + &chars.collect::<String>() + } + }) + .collect::<Vec<_>>() + .join(" ") +} + +#[test] +fn example() { + assert_eq!(title_capital("PERL IS gREAT"), "Perl is Great"); + assert_eq!( + title_capital("THE weekly challenge"), + "The Weekly Challenge" + ); + assert_eq!(title_capital("YoU ARE A stAR"), "You Are a Star"); +} |
