Skip to content

Instantly share code, notes, and snippets.

@keiichiroy
Created January 29, 2020 03:09
Show Gist options
  • Select an option

  • Save keiichiroy/14d3c7f5072617ca2f2a0cfe5ca4ff05 to your computer and use it in GitHub Desktop.

Select an option

Save keiichiroy/14d3c7f5072617ca2f2a0cfe5ca4ff05 to your computer and use it in GitHub Desktop.
Calculating JAN/EAN check digit
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