Skip to content

Instantly share code, notes, and snippets.

@BillOTei
Created October 4, 2014 06:37
Show Gist options
  • Save BillOTei/e91286ad7a000b54e2d8 to your computer and use it in GitHub Desktop.
Save BillOTei/e91286ad7a000b54e2d8 to your computer and use it in GitHub Desktop.
Luhn algorithm in python
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