Created
December 3, 2011 15:24
-
-
Save kirelagin/1427370 to your computer and use it in GitHub Desktop.
ICAO Doc 9303 checksum calculator
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
#!/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