-
-
Save LouisAmon/497df89d713f2d154b52420c21551a18 to your computer and use it in GitHub Desktop.
python mod10 checker
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
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment