Created
July 27, 2025 08:12
-
-
Save elliotwutingfeng/ce1ea4225ddcceccf93dd1cac3f47e4d to your computer and use it in GitHub Desktop.
ISO/IEC 7501-1 Passport Checksum
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
| 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