--- id: 6a0dcd03ee4e68698080ef6c title: "Challenge 308: Credit Card Validator" challengeType: 29 dashedName: challenge-308 --- # --description-- Given a string of digits for a credit card number, determine if it's a valid card number using the following method: - Starting from the second-to-last digit, double every other digit moving left. - If doubling a digit results in a number greater than 9, subtract 9. - Sum all the digits (doubled and undoubled). - If the total is divisible by 10, the number is valid. # --hints-- `is_valid_card("4532015112830366")` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_card("4532015112830366"), True)`) }}) ``` `is_valid_card("5425233430109903")` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_card("5425233430109903"), True)`) }}) ``` `is_valid_card("371449635398431")` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_card("371449635398431"), True)`) }}) ``` `is_valid_card("6011111111111117")` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_card("6011111111111117"), True)`) }}) ``` `is_valid_card("4532015112830367")` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_card("4532015112830367"), False)`) }}) ``` `is_valid_card("1234567890123456")` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_card("1234567890123456"), False)`) }}) ``` `is_valid_card("4532015112830368")` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_card("4532015112830368"), False)`) }}) ``` # --seed-- ## --seed-contents-- ```py def is_valid_card(number): return number ``` # --solutions-- ```py def is_valid_card(number): total = 0 for i, digit in enumerate(reversed(number)): d = int(digit) if i % 2 == 1: d *= 2 if d > 9: d -= 9 total += d return total % 10 == 0 ```