Exercise: Luhn Algorithm

The Luhn algorithm is used to validate credit card numbers. The algorithm takes a string as input and does the following to validate the credit card number:

  • Ignore all spaces. Reject numbers with fewer than two digits.

  • Moving from right to left, double every second digit: for the number 1234, we double 3 and 1. For the number 98765, we double 6 and 8.

  • After doubling a digit, sum the digits if the result is greater than 9. So doubling 7 becomes 14 which becomes 1 + 4 = 5.

  • Sum all the undoubled and doubled digits.

  • The credit card number is valid if the sum ends with 0.

The provided code provides a buggy implementation of the luhn algorithm, along with two basic unit tests that confirm that most of the algorithm is implemented correctly.

Copy the code below to https://play.rust-lang.org/ and write additional tests to uncover bugs in the provided implementation, fixing any bugs you find.

  1. #![allow(unused)]
  2. fn main() {
  3. pub fn luhn(cc_number: &str) -> bool {
  4.     let mut sum = 0;
  5.     let mut double = false;
  6.     for c in cc_number.chars().rev() {
  7.         if let Some(digit) = c.to_digit(10) {
  8.             if double {
  9.                 let double_digit = digit * 2;
  10.                 sum +=
  11.                     if double_digit > 9 { double_digit - 9 } else { double_digit };
  12.             } else {
  13.                 sum += digit;
  14.             }
  15.             double = !double;
  16.         } else {
  17.             continue;
  18.         }
  19.     }
  20.     sum % 10 == 0
  21. }
  22. #[cfg(test)]
  23. mod test {
  24.     use super::*;
  25.     #[test]
  26.     fn test_valid_cc_number() {
  27.         assert!(luhn("4263 9826 4026 9299"));
  28.         assert!(luhn("4539 3195 0343 6467"));
  29.         assert!(luhn("7992 7398 713"));
  30.     }
  31.     #[test]
  32.     fn test_invalid_cc_number() {
  33.         assert!(!luhn("4223 9826 4026 9299"));
  34.         assert!(!luhn("4539 3195 0343 6476"));
  35.         assert!(!luhn("8273 1232 7352 0569"));
  36.     }
  37. }
  38. }