Created
March 31, 2020 20:44
-
-
Save dasmido/6b65f061a93f1d6778fe966472e02b62 to your computer and use it in GitHub Desktop.
This file contains 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
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