Skip to content

Instantly share code, notes, and snippets.

@aechiara
Forked from henriquebastos/cpfchecksum.py
Last active August 29, 2015 13:56
Show Gist options
  • Save aechiara/9042563 to your computer and use it in GitHub Desktop.
Save aechiara/9042563 to your computer and use it in GitHub Desktop.
# coding: utf-8
def cpf_checksum(cpf):
"""
CPF Checksum algorithm.
"""
if cpf in map(lambda x: str(x) * 11, range(0, 10)):
return False
def dv(partial):
s = sum(b * int(v) for b, v in zip(range(len(partial)+1, 1, -1), partial))
return s % 11
dv1 = 11 - dv(cpf[:9])
q2 = dv(cpf[:10])
dv2 = 11 - q2 if q2 >= 2 else 0
return dv1 == int(cpf[9]) and dv2 == int(cpf[10])
def tests():
assert cpf_checksum('11144477735') == True
assert cpf_checksum('21111111120') == True
assert cpf_checksum('00000000000') == False
assert cpf_checksum('11111111111') == False
assert cpf_checksum('22222222222') == False
tests()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment