OneCompiler

Chile-GenID

113

import random

def calculate_check_digit(rut):
# Reverse the RUT and turn it into a list of integers
rut_reversed = list(map(int, str(rut)[::-1]))

# Define the weighting sequence {2, 3, 4, 5, 6, 7}
weighting_factors = [2, 3, 4, 5, 6, 7]

# Compute the sum of the products
total_sum = 0
for i, digit in enumerate(rut_reversed):
    total_sum += digit * weighting_factors[i % len(weighting_factors)]

# Calculate the remainder when dividing by 11
remainder = total_sum % 11
check_digit = 11 - remainder

# Adjust the check digit for special cases
if check_digit == 11:
    return '0'
elif check_digit == 10:
    return 'K'
else:
    return str(check_digit)

def generate_chilean_rut():
# Generate a random RUT number (7 to 8 digits for individuals)
rut_number = random.randint(1000000, 99999999)

# Calculate the check digit
check_digit = calculate_check_digit(rut_number)

# Combine the RUT number and check digit
return f"{rut_number}-{check_digit}"

Example of generating a RUT

generated_rut = generate_chilean_rut()
print(f"Generated Chilean RUT: {generated_rut}")