Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 27th, 2012  |  syntax: Python  |  size: 0.61 KB  |  views: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import sys
  2.  
  3. def add_number(x):
  4.   # double every other
  5.   d = []
  6.   for o, n in enumerate(x):
  7.     if (o % 2):
  8.       d.append(int(n)*2)
  9.     else:
  10.       d.append(int(n))
  11.   # sum the numbers
  12.   total = 0
  13.   for n in d:
  14.     for c in str(n):
  15.       total += int(c)
  16.   return total
  17.  
  18. def add_check(x):
  19.   print "{} check: {}".format(x, add_number(x) * 9 % 10)
  20.  
  21. def verify_check(x):
  22.   total = add_number(x[:-1])
  23.   if (total * 9 % 10) == int(x[-1]):
  24.     return True
  25.   return False
  26.  
  27. if __name__ == '__main__':
  28.   if len(sys.argv) != 2:
  29.     add_check(sys.argv[2])
  30.   else:
  31.     print verify_check(sys.argv[1])