Instantly share code, notes, and snippets.

Embed
What would you like to do?
Función para validar un CBU
def validate_cbu(cbu):
"""
Validation function for the CBU (Clave Bancaria Unica)
Arguments:
cbu -- String or int of 22 characters
"""
cbu = str(cbu)
if len(cbu) == 22:
sum1 = sum2 = 0
# Multipliers used to verify the CBU
multiplier = '7139713'
multiplier_2 = '3971397139713'
# We need to sum and multiply with the multiplier the first 7 characters
for i, num in enumerate(cbu[:7]):
sum1 += int(num) * int(multiplier[i])
# We need to sum and multiply with the multiplier from character 8 to 21
for i, num in enumerate(cbu[8:21]):
sum2 += int(num) * int(multiplier_2[i])
# If 10 minus the last character of sum1 is different from CBU char 8
# or if 10 minus the last character of sum2 is different from last CBU char
# we return False
if 10-int(str(sum1)[-1]) != int(cbu[7]) or 10-int(str(sum2)[-1]) != int(cbu[21]):
return False
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment