Implement a function, called is_credit_card_valid(number), which returns True or False based on the following algorithm:
- Starting from the right, double every second digit.
- If after doubling the digit becomes
>= 10, calculate the sum of the digits of the new number & use that. For example, if we have9and doubling it gets us18, the final result is going to be1 + 8 = 9 - After the transformation, we find the sum of all digits in the transformed number.
- The number is valid, if the sum is divisible by 10.
This is also known as the Luhn algorithm.
For example: 79927398713 is valid, bacause:
- After doubling, we get the following number:
7 +
sum_digits(9 * 2) +
9 +
sum_digits(2 * 2) +
7 +
sum_digits(3 * 2) +
9 +
sum_digits(8 * 2) +
7 +
sum_digits(1 * 2) +
3
- And calculating the sum of the digits of the transformed number, we get
70=> the card number is valid.
Signature
def is_credit_card_valid(number):
passExamples
is_credit_card_valid(79927398713) is True
is_credit_card_valid(79927398715) is False
is_credit_card_valid(4417123456789113) is TrueIn case you need more cards, you can check out Stripe testing cards