Created
December 1, 2023 21:25
-
-
Save paulovillarroel/5a87e13539b35a31b5d6707e2be5ed13 to your computer and use it in GitHub Desktop.
Function to calculate the verification digit of a RUT (for Chile)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate_dv(rut): | |
rut_str = str(rut) | |
if len(rut_str) not in [7, 8]: | |
raise ValueError("The RUT must have 7 or 8 digits.") | |
rut_str = rut_str.zfill(8)[-8:] | |
rut_rev = list(map(int, list(rut_str[::-1]))) | |
multiplier = [2, 3, 4, 5, 6, 7, 2, 3, 4] | |
dv = (11 - sum(x * y for x, y in zip(rut_rev, multiplier)) % 11) % 11 | |
return "K" if dv == 10 else str(dv) | |
# Example of use | |
rut_example = 23024485 | |
dv_calculated = calculate_dv(rut_example) | |
# Print result | |
print("The verification digit of the RUT", rut_example, "is:", dv_calculated) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment