Last active
April 7, 2017 08:49
-
-
Save tomazas/ade547d13ded07f7cb472b3896ae871e to your computer and use it in GitHub Desktop.
Luhn python implementation
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
# Luhn python implementation: http://www.ee.unb.ca/tervo/ee4253/luhn.shtml | |
def luhn(input): | |
sum = 0 | |
for i,c in enumerate(input): | |
num = (2-(i % 2)) * int(c) | |
sum += int(num/10) + (num % 10) | |
#print sum | |
return ((sum % 10) == 0) | |
# test | |
if __name__ == "__main__": | |
print "ok:",luhn("4563960122001999") | |
print "bad:",luhn("4563960122001998") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment