Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dígito verificador del RUT en Python
# encoding=utf-8
# Obtener el dígito verificador del RUT en Python.
#
# La función recibe el RUT como un entero,
# y entrega el dígito verificador como un entero.
# Si el resultado es 10, el RUT es "raya k".
from itertools import cycle
def digito_verificador(rut):
reversed_digits = map(int, reversed(str(rut)))
factors = cycle(range(2, 8))
s = sum(d * f for d, f in zip(reversed_digits, factors))
return (-s) % 11
@henhiskan

This comment has been minimized.

Copy link

commented Jul 12, 2013

me acabas de ahorrar tiempo, gracias bomba

@nguaman

This comment has been minimized.

Copy link

commented May 26, 2015

Gracias!

@ALXTorresC

This comment has been minimized.

Copy link

commented Oct 26, 2015

Hermano... me ahorraste montón de tiempo... ahora cabe preguntarte... puedo usarlo en mi proyecto de titulo para validar mis formularios?

@jrovegno

This comment has been minimized.

Copy link

commented Jan 19, 2017

En Ruby:

def calcula_dv(rut_base)
    reversed_digits = rut_base.to_s.reverse.chars.map(&:to_i)
    factors = (2..7).to_a.cycle
    suma = reversed_digits.zip(factors).inject(0) {|sum, (d, f)| sum + d * f }
    dv = (-suma) % 11 == 10 ? 'K' : (-suma) % 11
end
@aldrinmartoq

This comment has been minimized.

Copy link

commented Dec 22, 2017

Gracias @jrovegno ;-)

@vimavis

This comment has been minimized.

Copy link

commented Jan 9, 2018

vale compadre, te pasaste 👍

@ShelbyCobraGT500

This comment has been minimized.

Copy link

commented May 18, 2018

Hola sabes que me da ese error :
como puedo solucionarlo

Traceback (most recent call last):
File "", line 1, in
File "", line 7, in udf_rn103_validar_dv
File "", line 7, in
ValueError: invalid literal for int() with base 10: 'K'

@DiegoArrieta

This comment has been minimized.

Copy link

commented May 20, 2019

Muy bueno! agradecido, no es malo validarlo del lado del servidor.

@unho

This comment has been minimized.

Copy link

commented Jun 4, 2019

Es para el RUT de Chile o para el de Uruguay?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.