Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit fc9bd99

Browse files
committed
Add files via upload
1 parent 9289db0 commit fc9bd99

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Week2/validator_CNP.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
cnp = []
2+
ok = False
3+
4+
def dict_get_value(s, cnp_0):
5+
for keys, value in s.items():
6+
if cnp_0 in keys:
7+
return value
8+
return None
9+
10+
def birth_year(cnp):
11+
saa = {('1', '2', '7', '8', '9'): 1900, ('3', '4'): 1800, ('5', '6'): 2000}
12+
days_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
13+
14+
year = dict_get_value(saa, cnp[0]) + int(cnp[1:3])
15+
month = int(cnp[3:5])
16+
day = int(cnp[5:7])
17+
18+
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
19+
days_month[2] = 29
20+
return 1 <= month <= 12 and 1 <= day <= days_month[month]
21+
22+
def control(cnp):
23+
num = '279146358279'
24+
sum = 0
25+
26+
### SUM & MODULO ###
27+
for i in range(0, 12):
28+
sum = sum + int(cnp[i]) * int(num[i])
29+
c = sum % 11
30+
if c == 10:
31+
c = 1
32+
### OUTPUT ###
33+
34+
if c == int(cnp[12]):
35+
return True
36+
else:
37+
return False
38+
39+
while not ok:
40+
ok = True
41+
cnp = input("Te rugam sa introduci condul numeric personal:")
42+
if cnp[0] == 0 or len(cnp) != 13 or not cnp.isdigit():
43+
print("Cod numeric personal invalid.")
44+
ok = False
45+
46+
47+
elif not birth_year(cnp):
48+
ok = False
49+
elif int(cnp[7:9]) not in range(1, 53):
50+
ok = False
51+
elif cnp[9:12] == '000':
52+
ok = False
53+
elif not control(cnp):
54+
ok = False
55+
elif not ok:
56+
print("CNP gresit")
57+
58+
print("Ai introdus un CNP Valid.")

0 commit comments

Comments
 (0)