-
Notifications
You must be signed in to change notification settings - Fork 14
/
luhn.py
43 lines (36 loc) · 1.01 KB
/
luhn.py
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
38
39
40
41
42
43
__version__ = '0.2.0'
def checksum(string):
"""
Compute the Luhn checksum for the provided string of digits. Note this
assumes the check digit is in place.
"""
digits = list(map(int, string))
odd_sum = sum(digits[-1::-2])
even_sum = sum([sum(divmod(2 * d, 10)) for d in digits[-2::-2]])
return (odd_sum + even_sum) % 10
def verify(string):
"""
Check if the provided string of digits satisfies the Luhn checksum.
>>> verify('356938035643809')
True
>>> verify('534618613411236')
False
"""
return (checksum(string) == 0)
def generate(string):
"""
Generate the Luhn check digit to append to the provided string.
>>> generate('35693803564380')
9
>>> generate('53461861341123')
4
"""
cksum = checksum(string + '0')
return (10 - cksum) % 10
def append(string):
"""
Append Luhn check digit to the end of the provided string.
>>> append('53461861341123')
'534618613411234'
"""
return string + str(generate(string))