Skip to content

Instantly share code, notes, and snippets.

@kirelagin
Created December 3, 2011 15:24
Show Gist options
  • Save kirelagin/1427370 to your computer and use it in GitHub Desktop.
Save kirelagin/1427370 to your computer and use it in GitHub Desktop.
ICAO Doc 9303 checksum calculator
#!/usr/bin/env python3
from itertools import cycle
def chksum(val):
def tonum(v):
if len(v) == 1:
if v == '<':
return 0
c = ord(v)
if ord('0') <= c <= ord('9'):
return c - ord('0')
if ord('A') <= c <= ord('Z'):
return c - ord('A') + 10
raise ValueError('Invalid character')
val = str(val)
return sum(map(lambda p: p[0]*p[1],zip(map(tonum, val), cycle([7, 3, 1])))) % 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment