Skip to content

Instantly share code, notes, and snippets.

@dasmido
Created March 31, 2020 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dasmido/6b65f061a93f1d6778fe966472e02b62 to your computer and use it in GitHub Desktop.
Save dasmido/6b65f061a93f1d6778fe966472e02b62 to your computer and use it in GitHub Desktop.
import unittest
def luhn_checksum(card_number):
def digits_of(n):
return [int(d) for d in str(n)]
digits = digits_of(card_number)
odd_digits = digits[-1::-2]
even_digits = digits[-2::-2]
checksum = 0
checksum += sum(odd_digits)
for d in even_digits:
checksum += sum(digits_of(d*2))
return checksum % 10
def is_luhn_valid(card_number):
return luhn_checksum(card_number) == 0
#result = is_luhn_valid(1800905433149)
#print ("Correct:" + str(result))
#result = is_luhn_valid(6011514433546201)
#print ("Correct:" + str(result))
#result = is_luhn_valid(6771549495586802)
#print ("Correct:" + str(result))
#assert str(result) == 'True', 'should be True'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment