Created
October 4, 2014 06:37
-
-
Save BillOTei/e91286ad7a000b54e2d8 to your computer and use it in GitHub Desktop.
Luhn algorithm in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def validate(n): | |
def process_1(index, nb, odd): | |
result = (nb * 2 if index % 2 else nb) if odd else (nb * 2 if 0 == index % 2 else nb) | |
return result - 9 if result > 9 else result | |
n = [int(i) for i in str(n)] | |
if len(n) % 2: | |
n = [process_1(index, nb, True) for index, nb in enumerate(n)] | |
else: | |
n = [process_1(index, nb, False) for index, nb in enumerate(n)] | |
return sum(n) % 10 == 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment