Skip to content

Instantly share code, notes, and snippets.

@emesik
Created April 29, 2011 16:37
Show Gist options
  • Save emesik/948594 to your computer and use it in GitHub Desktop.
Save emesik/948594 to your computer and use it in GitHub Desktop.
python mod10 checker
def mod10(number):
digits = []
even = False
for digit in reversed(number):
digit = ord(digit) - ord('0')
if even:
digit = digit * 2
if digit >= 10:
digit = digit % 10 + digit / 10
digits.append(digit)
even = not even
return sum(digits) % 10 == 0
if __name__ == '__main__':
# valid
print mod10('370000000000002')
print mod10('6011000000000012')
print mod10('4007000000027')
print mod10('4012888818888')
print mod10('3088000000000017')
print mod10('38000000000006')
# invalid
print mod10('3700000000000002')
print mod10('6011001000000012')
print mod10('4006000000027')
print mod10('4012886818888')
print mod10('3088020000000017')
print mod10('38000000030006')
@dhulke
Copy link

dhulke commented Oct 16, 2017

I think you could change digit = digit % 10 + digit / 10 to digit = digit % 10 + 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment