Last active
February 24, 2020 18:32
-
-
Save utsengar/7b49dc06462007752d30 to your computer and use it in GitHub Desktop.
Validate routing number in Python
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
""" | |
The function checks correctness of a routing number using the Checksum algorithm. | |
Checksum algorithm: http://en.wikipedia.org/wiki/Routing_transit_number#Check_digit | |
""" | |
def validate(routing_number): | |
if len(s) != 9: | |
return False | |
n = 0 | |
for i in xrange(0, len(s), 3): | |
n += int(s[i]) * 3 | |
n += int(s[i + 1]) * 7 | |
n += int(s[i + 2]) | |
if n != 0 and n % 10 == 0: | |
return True | |
else: | |
return False |
ofcourse, more pythonic :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nitpicky but how about swapping:
for
return n != 0 and n % 10 == 0