-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluhn.py
More file actions
37 lines (30 loc) · 946 Bytes
/
luhn.py
File metadata and controls
37 lines (30 loc) · 946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
Luhn
"""
class Luhn:
"""
Luhn algorithm
"""
def __init__(self, card_num):
self.card_num = card_num.replace(' ', '')
def valid(self):
"""
Given a number determine whether or not it is valid per the Luhn formula.
index manipulation would be more complex:
card = [int(value) for value in self.card_num]
card[-2::-2] = [2*value - 9*(value>4) for value in card[-2::-2]]
"""
total = 0
index = 1
parity = len(self.card_num) % 2
for index, value in enumerate(self.card_num, start=1):
if value.isdigit():
if index % 2 == parity:
total += int(value)
elif int(value) > 4:
total += 2*int(value) - 9
else:
total += 2*int(value)
else:
return False
return total % 10 == 0 and index > 1