-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpf_validation.py
More file actions
24 lines (16 loc) · 967 Bytes
/
cpf_validation.py
File metadata and controls
24 lines (16 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import re
class ValidateCpfNumber:
def validate(self,cpf: str) -> bool:
# Salva cada dígito na variável numbers
numbers = [int(digit) for digit in cpf if digit.isdigit()]
# Efetua o cálculo da soma dos produtos de cada dígito
sum_of_products = sum(a*b for a, b in zip(numbers[0:9], range(10, 1, -1)))
expected_digit1 = (sum_of_products * 10 % 11) % 10
sum_of_products = sum(a*b for a, b in zip(numbers[0:10], range(11, 1, -1)))
expected_digit2 = (sum_of_products * 10 % 11) % 10
if (re.match("^[0-9]{11}$", cpf) or re.match(r'\d{3}.\d{3}.\d{3}-\d{2}$', cpf)) and not len(set(numbers)) == 1 and numbers[9] == expected_digit1 and numbers[10] == expected_digit2:
print('O C.P.F. '+ str(cpf) + ' é válido!')
return True
else:
print('O C.P.F. '+ str(cpf) + ' não é válido!')
return False