Skip to content

Instantly share code, notes, and snippets.

@alexkiro
Last active December 20, 2019 12:45
Show Gist options
  • Save alexkiro/3af9afff156fbc1ea954e9ec2d6a4f4a to your computer and use it in GitHub Desktop.
Save alexkiro/3af9afff156fbc1ea954e9ec2d6a4f4a to your computer and use it in GitHub Desktop.
def validate_cif(cif):
# Remove RO if it was added
cif = str(cif).lower().lstrip("ro")
if not cif.isnumeric():
# Must be numeric
return False
if len(cif) >= 10:
# Too long
return False
if len(cif) <= 1:
# Too short
return False
key = "753217532"
computed_check_number = sum(
int(i) * int(j)
for i, j in zip(cif[::-1][1:], key[::-1])
) * 10 % 11
# Modulo by 10, to handle the case where the check number
# is 10.
if computed_check_number % 10 != int(cif[-1]):
# Self check number isn't correct
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment