Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[test]
- fn return_3() {
- assert_eq!(3, captcha("1122"))
- }
- #[test]
- fn return_4() {
- assert_eq!(4, captcha("1111"))
- }
- #[test]
- fn return_0() {
- assert_eq!(0, captcha("1234"))
- }
- fn captcha(number_string: &str) -> u32 {
- let mut out : u32 = 0;
- let mut number_chars = number_string.chars().peekable();
- let first = number_chars.nth(0);
- let mut this_char = first;
- loop {
- match this_char {
- Some(i) => {
- match number_chars.peek() {
- Some(j) => {
- if i == *j {
- out += i.to_digit(10).unwrap();
- }
- },
- None => {
- if i == first.unwrap() {
- out += i.to_digit(10).unwrap();
- }
- break;
- }
- };
- },
- None => ()
- };
- this_char = number_chars.next();
- }
- out
- }
Add Comment
Please, Sign In to add comment