-
Notifications
You must be signed in to change notification settings - Fork 0
/
tckn.py
40 lines (34 loc) · 1.36 KB
/
tckn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import random
import string
def generate() -> str:
"""Generate a random valid identity number."""
digits = range(10)
first_digit = random.randint(1, 9) # Must be nonzero
first_9_digits = [first_digit] + random.choices(digits, k=8)
first_part = ''.join(str(d) for d in first_9_digits)
return first_part + last_2_digits(first_part)
def validate(number: str) -> bool:
"""Check if a string is a valid identity number or not."""
if len(number) != 11:
return False
if number[0] == '0':
return False
if not all(d in string.digits for d in number):
return False
digits = [int(d) for d in number]
if digits[9] != (sum(digits[:9:2])*7 - sum(digits[1:9:2])) % 10:
return False
if digits[10] != sum(digits[:10]) % 10:
return False
return True
def last_2_digits(number: str) -> str:
"""Generate last 2 digits for first 9 digits of an identity number."""
if len(number) != 9:
raise ValueError('The number should have 9 digits')
if not all(d in string.digits for d in number):
raise ValueError('The characters must be all digits')
first_digits = [int(d) for d in number]
digit_10 = (sum(first_digits[::2])*7 - sum(first_digits[1::2])) % 10
first_10_digits = first_digits + [digit_10]
digit_11 = sum(first_10_digits) % 10
return str(digit_10) + str(digit_11)