Skip to content

Latest commit

 

History

History
24 lines (19 loc) · 834 Bytes

03 Credit Card Validation.md

File metadata and controls

24 lines (19 loc) · 834 Bytes

Lab 03: Credit Card Validation

Let's write a function which returns whether a string containing a credit card number is valid as a boolean. The steps are as follows:

  1. Convert the input string into a list of ints
  2. Slice off the last digit. That is the check digit.
  3. Reverse the digits.
  4. Double every other element in the reversed list.
  5. Subtract nine from numbers over nine.
  6. Sum all values.
  7. Take the second digit of that sum.
  8. If that matches the check digit, the whole card number is valid.

For example, the worked out steps would be:

  1. 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5 5
  2. 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5
  3. 5 8 9 9 8 6 8 5 7 3 7 6 5 5 4
  4. 10 8 18 9 16 6 16 5 14 3 14 6 10 5 8
  5. 1 8 9 9 7 6 7 5 5 3 5 6 1 5 8
  6. 85
  7. 5
  8. Valid!