Skip to content

Instantly share code, notes, and snippets.

@enihsyou
Created June 19, 2018 09:53
Show Gist options
  • Save enihsyou/c55e83e56a6c3c7800f833642d225035 to your computer and use it in GitHub Desktop.
Save enihsyou/c55e83e56a6c3c7800f833642d225035 to your computer and use it in GitHub Desktop.
身份证简单校验
# -*- coding: utf-8 -*-
from typing import List
def check(array: List[str]):
assert len(array) == 18 # 身份证有18位
id_characters = "0123456789X"
sum_array = [2 ** (17 - i) % 11 * int(d) for (i, d) in enumerate(array[:17])]
check_sum = (12 - sum(sum_array) % 11) % 11
check_sum_str = id_characters[check_sum]
return array[17].upper() == check_sum_str
if __name__ == '__main__':
# trues
print(check(list("52032719890813555X")))
print(check(list("341702199007042360")))
print(check(list("450107199309127785")))
print(check(list("500234198305317092")))
print(check(list("500113198507058570")))
print()
# falses
print(check(list("520327198908135550")))
print(check(list("441702199007042360")))
print(check(list("450107199309127285")))
print(check(list("50023419830531709X")))
print(check(list("500113198507058540")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment