You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Python/README.md
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,4 +25,47 @@ Read more:
25
25
-[What is Checksum on a credit card?](https://www.sapling.com/7966257/checksum-credit-card)
26
26
27
27
### Python solution
28
+
The Python solution is very similar to the pseudocode written in generalised solution of `README.md`.
29
+
There are some Python specific functions/methods used which makes the code a lot more compact.
28
30
31
+
To begin with, we get the credit card number from the user and instantly convert it to a list.
32
+
We then use `pop()`, a function that removes, and then returns, the last element of a list.
33
+
We assign the return of `pop()`, the last number in `credit_card_number`, to `last_digit`.
34
+
```python
35
+
# Get input, define last_digit, and remove the 16th digit from the credit card.
36
+
credit_card_number =list(input("Please enter the credit card number: "))
37
+
last_digit = credit_card_number.pop()
38
+
```
39
+
40
+
Next is the meat of our code.
41
+
We use `enumerate()` which, in Python, gives us both `i` the current index and `number` the current value at that index (e.g. the current number in `credit_card_number`).
42
+
For each number in `credit_card_number` we do the following:
43
+
1. Check if the index `i` of the `number` is even. If so we multiply that number by 2.
44
+
2. We check if the current number is longer than 1 digit. If so we break up the number and add each digit together.
45
+
3. Ensure every number in `credit_card_number` is an integer and not a string because later on we use `sum()` which doesn't like strings.
46
+
```python
47
+
# Make use of enumerate to get the index and value of each number in the credit card number.
48
+
for i, number inenumerate(credit_card_number):
49
+
if i %2==0: # if the index is even (i.e. every other element)
50
+
credit_card_number[i] =int(number) *2
51
+
52
+
# Check is the length of the number is more than a single digit.
53
+
iflen(str(credit_card_number[i])) >1:
54
+
# Break up the number into two digits and add them together
55
+
# Example: 12 := 1 + 2 = 3
56
+
x =str(credit_card_number[i])[0]
57
+
y =str(credit_card_number[i])[1]
58
+
credit_card_number[i] =int(x) +int(y) # We replace the previous number with our new single digit one.
59
+
60
+
# Here we make sure every element is converted to an integer otherwise we have issues with sum() later on.
61
+
credit_card_number[i] =int(credit_card_number[i])
62
+
```
63
+
64
+
Finally, we check if the `credit_card_number` is valid.
65
+
We add the sum of the `credit_card_number` and add the `last_digit` to it.
66
+
We then check if the result is a multiple of 10 using `% 10 === 0`.
67
+
```python
68
+
# Add up all the numbers, add the 16th digit, then check if final number is a multiple of 10.
0 commit comments