|
2 | 2 |
|
3 | 3 | Implement a function, called `is_credit_card_valid(number)`, which returns True/False based on the following algorithm:
|
4 | 4 |
|
5 |
| -* Each credit card number must contain odd count of digits. |
6 |
| -* We transform the number with the following steps (based on the fact that we start from index 0) |
| 5 | +- Each credit card number must contain odd count of digits. |
| 6 | +- We transform the number with the following steps (based on the fact that we start from index 0) |
7 | 7 | - All digits, read from right to left, at even positions (index), **remain the same.**
|
8 | 8 | - Every digit, read from right to left, at odd position is replaced by the result, that is obtained from doubling the given digit.
|
9 |
| -* After the transformation, we find the sum of all digits in the transformed number. |
10 |
| -* The number is valid, if the sum is divisible by 10. |
| 9 | +- After the transformation, we find the sum of all digits in the transformed number. |
| 10 | +- The number is valid, if the sum is divisible by 10. |
11 | 11 |
|
12 |
| -For example: `79927398713` is valid, bacause: |
| 12 | +For example: `79927398715` is valid, bacause: |
13 | 13 |
|
14 |
| -* When we double and replace all digits at odd position we get: `7 (18 = 2 * 9) 9 (4 = 2 * 2) 7 (6 = 2 * 3) 9 (16 = 2 * 8) 7 (2 = 2 * 1) 3` |
15 |
| -* The sum of all digits of the new number is 70, which is divisible by 10 => the card number is valid. |
| 14 | +- When we double and replace all digits at odd position we get: `7 (18 = 2 * 9) 9 (4 = 2 * 2) 7 (6 = 2 * 3) 9 (16 = 2 * 8) 7 (2 = 2 * 1) 5` |
| 15 | +- The sum of all digits of the new number is 90, which is divisible by 10 => the card number is valid. |
16 | 16 |
|
17 | 17 | More examples:
|
18 | 18 |
|
19 |
| -* `79927398713` is a valid number |
20 |
| -* `79927398715` is invalid number |
| 19 | +- `79927398713` is an invalid number |
| 20 | +- `79927398715` is a valid number |
0 commit comments