MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / is_polish_national_id

Function is_polish_national_id

strings/is_polish_national_id.py:1–86  ·  view source on GitHub ↗

Verification of the correctness of the PESEL number. www-gov-pl.translate.goog/web/gov/czym-jest-numer-pesel?_x_tr_sl=auto&_x_tr_tl=en PESEL can start with 0, that's why we take str as input, but convert it to int for some calculations. >>> is_polish_national_id(123)

(input_str: str)

Source from the content-addressed store, hash-verified

1def is_polish_national_id(input_str: str) -> bool:
2 """
3 Verification of the correctness of the PESEL number.
4 www-gov-pl.translate.goog/web/gov/czym-jest-numer-pesel?_x_tr_sl=auto&_x_tr_tl=en
5
6 PESEL can start with 0, that's why we take str as input,
7 but convert it to int for some calculations.
8
9
10 >>> is_polish_national_id(123)
11 Traceback (most recent call last):
12 ...
13 ValueError: Expected str as input, found <class 'int'>
14
15 >>> is_polish_national_id("abc")
16 Traceback (most recent call last):
17 ...
18 ValueError: Expected number as input
19
20 >>> is_polish_national_id("02070803628") # correct PESEL
21 True
22
23 >>> is_polish_national_id("02150803629") # wrong month
24 False
25
26 >>> is_polish_national_id("02075503622") # wrong day
27 False
28
29 >>> is_polish_national_id("-99012212349") # wrong range
30 False
31
32 >>> is_polish_national_id("990122123499999") # wrong range
33 False
34
35 >>> is_polish_national_id("02070803621") # wrong checksum
36 False
37 """
38
39 # check for invalid input type
40 if not isinstance(input_str, str):
41 msg = f"Expected str as input, found {type(input_str)}"
42 raise ValueError(msg)
43
44 # check if input can be converted to int
45 try:
46 input_int = int(input_str)
47 except ValueError:
48 msg = "Expected number as input"
49 raise ValueError(msg)
50
51 # check number range
52 if not 10100000 <= input_int <= 99923199999:
53 return False
54
55 # check month correctness
56 month = int(input_str[2:4])
57
58 if (
59 month not in range(1, 13) # year 1900-1999
60 and month not in range(21, 33) # 2000-2099

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected