-
-
Save diegoponciano/a54dca1e79a6045e5eb1 to your computer and use it in GitHub Desktop.
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 next_digit(value, base): | |
return value + str(sum(int(a)*b for a,b in zip(value, base))%11%10) | |
def make_valid(value, ap2, base): | |
return next_digit(next_digit(value, base), ap2+base) | |
def numbers_only(string): | |
return ''.join([s for s in string if s.isdigit()]) | |
def is_valid_cpf(cpf): | |
cpf = numbers_only(cpf) | |
return make_valid(cpf[:9], [0], [1,2,3,4,5,6,7,8,9]) == cpf | |
def is_valid_cnpj(cnpj): | |
cnpj = numbers_only(cnpj) | |
return make_valid(cnpj[:12], [5], [6,7,8,9,2,3,4,5,6,7,8,9]) == cnpj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment