Skip to content

Commit 6455b39

Browse files
authored
Create 07 Credit Card Validation.md
1 parent ba23ce5 commit 6455b39

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Lab 7: Credit Card Validation
2+
3+
4+
Let's write a function `credit_card_validator` which returns whether a string containing a credit card number is valid as a boolean. The steps are as follows:
5+
6+
1. Convert the input string into a list of ints
7+
2. Slice off the last digit. That is the **check digit**.
8+
3. Reverse the digits.
9+
4. Double every other element in the reversed list (starting with the first number in the list).
10+
5. Subtract nine from numbers over nine.
11+
6. Sum all values.
12+
7. Take the second digit of that sum.
13+
8. If that matches the check digit, the whole card number is valid.
14+
15+
Here is a valid credit card number to test with: 4556737586899855
16+
17+
For example, the worked out steps would be:
18+
19+
1. `4 5 5 6 7 3 7 5 8 6 8 9 9 8 5 5`
20+
2. `4 5 5 6 7 3 7 5 8 6 8 9 9 8 5`
21+
3. `5 8 9 9 8 6 8 5 7 3 7 6 5 5 4`
22+
4. `10 8 18 9 16 6 16 5 14 3 14 6 10 5 8`
23+
5. `1 8 9 9 7 6 7 5 5 3 5 6 1 5 8`
24+
6. 85
25+
7. 5
26+
8. `True` Valid!

0 commit comments

Comments
 (0)