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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
import re
import calendar
IDCARD_REGEX = '[1-9][0-9]{14}([0-9]{2}[0-9X])?'
def is_valid_idcard(idcard):
"""Validate id card is valid."""
if isinstance(idcard, int):
idcard = str(idcard)
if not re.match(IDCARD_REGEX, idcard):
return False
# 将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:
factors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
items = [int(item) for item in idcard[:-1]]
# 将这17位数字和系数相乘的结果相加
copulas = sum([a * b for a, b in zip(factors, items)])
# 用加出来和除以11,看余数是多少? 余数只可能有0-1-2-3-4-5-6-7-8-9-10这11个数字。 其分别对应的最后一位身份证的号码为:
ckcodes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
return ckcodes[copulas % 11].upper() == idcard[-1].upper()
def show_possibility_month_day(year):
for month in range(1, 13):
m = '0' + str(month) if len(str(month)) < 2 else str(month)
for day in range(calendar.monthrange(year, month)[1] + 1)[1:]:
d = '0' + str(day) if len(str(day)) < 2 else str(day)
r = '110101'+str(year)+m+d+'0539'
if is_valid_idcard(r):
print(r)
if __name__ == '__main__':
show_possibility_month_day(1999)
# 符合条件的可能
'''
110101199901010539
110101199901280539
110101199902080539
110101199902160539
110101199902240539
110101199903040539
110101199903120539
110101199903200539
110101199904190539
110101199904270539
110101199905070539
110101199905150539
110101199905230539
110101199905310539
110101199906030539
110101199906110539
110101199907180539
110101199907260539
110101199908060539
110101199908140539
110101199908220539
110101199908300539
110101199909020539
110101199909100539
110101199909290539
110101199910080539
110101199910160539
110101199910240539
110101199911040539
110101199911120539
110101199911200539
110101199912190539
110101199912270539
'''
|