Skip to content

Instantly share code, notes, and snippets.

@elliotwutingfeng
Created July 27, 2025 08:12
Show Gist options
  • Select an option

  • Save elliotwutingfeng/ce1ea4225ddcceccf93dd1cac3f47e4d to your computer and use it in GitHub Desktop.

Select an option

Save elliotwutingfeng/ce1ea4225ddcceccf93dd1cac3f47e4d to your computer and use it in GitHub Desktop.
ISO/IEC 7501-1 Passport Checksum
import itertools
def checksum(s: str) -> int:
weight_generator = itertools.cycle([7, 3, 1])
total = 0
for c in s:
weight = next(weight_generator)
code_point = ord(c)
if code_point == 60: # <
value = 0
elif 48 <= code_point <= 57: # 0 - 9
value = code_point - 48
elif 65 <= code_point <= 90: # A - Z
value = code_point - 55
else:
raise ValueError("Only characters 0-9, A-Z and < are allowed.")
total += value * weight
return total % 10
print(checksum("A1234567Z"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment