Skip to content

Instantly share code, notes, and snippets.

@utsengar
Last active February 24, 2020 18:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utsengar/7b49dc06462007752d30 to your computer and use it in GitHub Desktop.
Save utsengar/7b49dc06462007752d30 to your computer and use it in GitHub Desktop.
Validate routing number in Python
"""
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
@NotBobTheBuilder
Copy link

Nitpicky but how about swapping:

if n != 0 and n % 10 == 0:
  return True
else:
  return False

for
return n != 0 and n % 10 == 0

@utsengar
Copy link
Author

utsengar commented Feb 5, 2015

ofcourse, more pythonic :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment