Skip to content

Instantly share code, notes, and snippets.

@benwrk
Last active June 7, 2024 10:38
Show Gist options
  • Save benwrk/f767fa107891e1ce65df23b0b3f0f385 to your computer and use it in GitHub Desktop.
Save benwrk/f767fa107891e1ce65df23b0b3f0f385 to your computer and use it in GitHub Desktop.
One-liner Thai National ID Card check digit calculator and validator
def calculate_id_card_check_digit(card_number: str) -> int:
return (11 - sum([(len(card_number) - i) * x for i, x in enumerate(map(lambda s: int(s), card_number[:-1]))]) % 11) % 10
def validate_id_card_number_check_digit(card_number: str) -> bool:
return int(card_number[-1:]) == calculate_id_card_check_digit(card_number)
def validate_id_card_number_check_digit(card_number: str) -> bool:
return int(card_number[-1:]) == (11 - sum([(len(card_number) - i) * x for i, x in enumerate(map(lambda s: int(s), card_number[:-1]))]) % 11) % 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment