For those who doesn't know, Chilean RUT is an ID for residents, similar to USCIS#, and is composed by 7-8 numbers followed by a verification digit (calculated by a type of MOD11 check).
I made the next python code to get that number using python lists, but I need to ask if there is a better, newest, smarter or shorter way to obtain it.
MOD11 algorithm used:
- ID number of 8 digits
- Reverse it
- Multiply each digit by 2,3,4,5,6,7,2,3 (i used a 10 length number to try in case of larger number)
- Sum the multiplied values and calculate the modulus(%) by 11
- The verification digit is 11 - modulus
- If digit is 10 show 'K', if it's 11 show '0'.
CODE:
# Function to calculate digit using python lists
def DIG(RUT):
RUTrev=list(reversed(RUT))
CAD10=list(str(2345672345))
calc = list(int(RUTrev[i]) * int(CAD10[i]) for i in range(len(RUT)))
digit = 11-sum(calc)%11
if digit==10:
digit="K"
elif digit==11:
digit=0
else:
digit=digit
return digit
# Using the function to show the entire ID
RUT=input("Insert RUT number:/n")
print(f"RUT with digit is: {RUT}-{DIG(RUT)}")
OUTPUT:
Insert RUT number:
12345678
RUT with digit is: 12345678-5
Firstly, you should make the return type of your function consistent.
You can enumerate the input value (RUT) easily with map()
Something like this:
Output: