Guest User

Untitled

a guest
Dec 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #[test]
  2. fn return_3() {
  3. assert_eq!(3, captcha("1122"))
  4. }
  5.  
  6. #[test]
  7. fn return_4() {
  8. assert_eq!(4, captcha("1111"))
  9. }
  10.  
  11. #[test]
  12. fn return_0() {
  13. assert_eq!(0, captcha("1234"))
  14. }
  15.  
  16. fn captcha(number_string: &str) -> u32 {
  17. let mut out : u32 = 0;
  18. let mut number_chars = number_string.chars().peekable();
  19. let first = number_chars.nth(0);
  20. let mut this_char = first;
  21. loop {
  22. match this_char {
  23. Some(i) => {
  24. match number_chars.peek() {
  25. Some(j) => {
  26. if i == *j {
  27. out += i.to_digit(10).unwrap();
  28. }
  29. },
  30. None => {
  31. if i == first.unwrap() {
  32. out += i.to_digit(10).unwrap();
  33. }
  34. break;
  35. }
  36. };
  37. },
  38. None => ()
  39. };
  40. this_char = number_chars.next();
  41. }
  42. out
  43. }
Add Comment
Please, Sign In to add comment