forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluhn_algorithm.py
More file actions
35 lines (27 loc) · 783 Bytes
/
Copy pathluhn_algorithm.py
File metadata and controls
35 lines (27 loc) · 783 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
"""
Luhn algorithm or Luhn formula is a simple checksum formula
used to validate a variety of identification numbers, such as
credit card numbers, IMEI numbers, National Provider Identifier numbers
in some of the Countries
"""
"""
It takes a number as an input
(Assuming cardnumber as a string)
and returns true or false
based upon whether number is valid or not
"""
def checkLuhn(cardNumber):
cardLen = len(cardNumber)
checkSum = 0
isParity = False
for digit in range(cardLen-1,-1,-1):
if isParity:
cal = int(cardNumber[digit])*2
else:
cal = int(cardNumber[digit])
if cal>9:
checkSum += cal -9
else:
checkSum += cal
isParity = not isParity
return checkSum%10==0