Created
January 29, 2020 03:09
-
-
Save keiichiroy/14d3c7f5072617ca2f2a0cfe5ca4ff05 to your computer and use it in GitHub Desktop.
Calculating JAN/EAN check digit
This file contains hidden or 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
| from functools import reduce | |
| string = '456995111617' | |
| odds = string[1::2] | |
| evens = string[0::2] | |
| sum_from_odds = reduce(lambda a,b: int(a)+int(b),odds) * 3 | |
| sum_from_evens = reduce(lambda a,b: int(a)+int(b),evens) | |
| value_to_minus = str(sum_from_odds + sum_from_evens)[-1] | |
| check_digit = str(10 - int(value_to_minus)) | |
| code = string + check_digit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment