Created
May 1, 2018 09:32
-
-
Save benhoyle/13ffb941f6d146e951723fb2ab57a80a to your computer and use it in GitHub Desktop.
Calculate EPO Check Digit given the application number as a string
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 calculate_checkdigit(appln_number): | |
"""Calculate EPO checkdigit.""" | |
def digits_of(n): | |
return [int(d) for d in str(n)] | |
digits = digits_of(appln_number) | |
even_digits = digits[-1::-2] | |
odd_digits = digits[-2::-2] | |
checksum = 0 | |
checksum += sum(odd_digits) | |
for d in even_digits: | |
checksum += sum(digits_of(d*2)) | |
check_digit = 10 - (checksum % 10) | |
if check_digit == 10: | |
check_digit = 0 | |
return check_digit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment