-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathrenavam.py
48 lines (39 loc) · 1.54 KB
/
renavam.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
41
42
43
44
45
46
47
48
def is_valid_renavam(renavam): # type: (str) -> bool
"""
Validates the Brazilian vehicle registration number (RENAVAM).
This function takes a RENAVAM string and checks if it is valid.
A valid RENAVAM consists of exactly 11 digits. Theast digit a check digit
calculated from the the first 10 digits using a specific weighting system.
Args:
renavam (str): The RENAVAM string to be validated.
Returns:
bool: True if the RENAVAM is valid, False otherwise.
Example:
>>> is_valid_renavam('35298206229')
True
>>> is_valid_renavam('12345678900')
False
>>> is_valid_renavam('1234567890a')
False
>>> is_valid_renavam('12345678 901')
False
>>> is_valid_renavam('12345678') # Less than 11 digits
False
>>> is_valid_renavam('') # Empty string
False
"""
if len(renavam) != 11 or not renavam.isdigit():
return False
## Calculating the check digit
digits = [int(digit) for digit in renavam[:10]] # 10 digits
weights = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
checksum = sum(
digit * weight for digit, weight in zip(digits, weights)
) # Sum of the products of the digits and weights
remainder = checksum % 11
check_digit = 0 if remainder == 0 else 11 - remainder
# If the calculated check digit is 0, return False
if check_digit == 0:
return False
# Checking if the calculated check digit is equal to the last digit of the RENAVAM
return int(renavam[-1]) == check_digit