Skip to content

Instantly share code, notes, and snippets.

@paulovillarroel
Created December 1, 2023 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulovillarroel/5a87e13539b35a31b5d6707e2be5ed13 to your computer and use it in GitHub Desktop.
Save paulovillarroel/5a87e13539b35a31b5d6707e2be5ed13 to your computer and use it in GitHub Desktop.
Function to calculate the verification digit of a RUT (for Chile)
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