Created
March 25, 2015 01:50
-
-
Save cauethenorio/02618a8655ba8bde1539 to your computer and use it in GitHub Desktop.
estudo validação DNI peruano
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 dni_validator_1(dni): | |
multiplos = '32765432' | |
numbers = list('67890112345') | |
letters = list('KABCDEFGHIJ') | |
dni = list(dni) | |
control = dni.pop() | |
suma = sum(int(char) * int(mul) for mul, char in zip(multiplos, dni)) | |
key = 11 - (suma % 11) | |
key %= 11 | |
return (numbers if control.isdigit() else letters)[key] == control | |
def dni_validator_2(dni): | |
if len(dni) != 9: | |
return False | |
dni = list(dni) | |
control = dni.pop() | |
getval = lambda c: ord(c) - ord('0' if control.isdigit() else 'A') | |
m = [7,3,1] | |
n = sum(getval(c) * m[i % 3] for i, c in enumerate(dni)) | |
return n % 10 == getval(control) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment