Function to luhn algorithm validation for a given credit card number. >>> luhn_validation('4111111111111111') True >>> luhn_validation('36111111111111') True >>> luhn_validation('41111111111111') False
(credit_card_number: str)
| 19 | |
| 20 | |
| 21 | def luhn_validation(credit_card_number: str) -> bool: |
| 22 | """ |
| 23 | Function to luhn algorithm validation for a given credit card number. |
| 24 | >>> luhn_validation('4111111111111111') |
| 25 | True |
| 26 | >>> luhn_validation('36111111111111') |
| 27 | True |
| 28 | >>> luhn_validation('41111111111111') |
| 29 | False |
| 30 | """ |
| 31 | cc_number = credit_card_number |
| 32 | total = 0 |
| 33 | half_len = len(cc_number) - 2 |
| 34 | for i in range(half_len, -1, -2): |
| 35 | # double the value of every second digit |
| 36 | digit = int(cc_number[i]) |
| 37 | digit *= 2 |
| 38 | # If doubling of a number results in a two digit number |
| 39 | # i.e greater than 9(e.g., 6 x 2 = 12), |
| 40 | # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), |
| 41 | # to get a single digit number. |
| 42 | if digit > 9: |
| 43 | digit %= 10 |
| 44 | digit += 1 |
| 45 | cc_number = cc_number[:i] + str(digit) + cc_number[i + 1 :] |
| 46 | total += digit |
| 47 | |
| 48 | # Sum up the remaining digits |
| 49 | for i in range(len(cc_number) - 1, -1, -2): |
| 50 | total += int(cc_number[i]) |
| 51 | |
| 52 | return total % 10 == 0 |
| 53 | |
| 54 | |
| 55 | def validate_credit_card_number(credit_card_number: str) -> bool: |
no outgoing calls
no test coverage detected