Last active
August 29, 2015 14:17
-
-
Save fabian57/e6a23e83a2095b179031 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
VALID_LENGTHS = dict() | |
for s in open("is_valid_iban_data.txt").read().split("\n"): | |
VALID_LENGTHS[s[:2]] = int(s[3:]) | |
def is_valid_iban(string): | |
iban = string.replace(" ", "").replace("-", "").upper() | |
if len(iban) == VALID_LENGTHS[iban[:2]]: | |
inverted_iban = iban[4:] + iban[:4] | |
number = "" | |
for c in inverted_iban: | |
if "A" <= c <= "Z": | |
number += str(ord(c)-55) | |
elif c in "0123456789": | |
number += c | |
else: | |
return False | |
return int(number) % 97 == 1 |
Ça plante toujours à la conversion, non?
EDIT: ah non, ça peut donner un résultat faux a priori. En effet, supprimer les mauvais caractères peut donner une chaîne dont le modulo 97 est OK quand même (il y a peu de chances, mais le bug est d'autant plus pernicieux!).
Ah oui, il y avait des problèmes pendant les tests du coup j'ai pas remarqué
Et je viens de découvrir que ça plante sur "gb82 west 1234 5698 7654 32"
Pour bien faire, il faudrait que j'ajoute un test où ta version valide alors que l'IBAN contient un mauvais caractère.
Oui, ça marche, par contre du coup ta batterie de tests est un poil maladroite. Tu peux éviter de tester deux fois la même expression je pense ;)
Oui, c'est ça!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow, c'est super. Ton programme a un seul défaut, il plante sur: