Skip to content

Instantly share code, notes, and snippets.

@angusb
Created March 21, 2013 20:14
Show Gist options
  • Save angusb/5216299 to your computer and use it in GitHub Desktop.
Save angusb/5216299 to your computer and use it in GitHub Desktop.
def str_to_int(input):
base = ord('0')
def char_to_int(c, d):
n = ord(d) - base
if not (0 <= n <= 9):
raise ValueError('Bad input')
return 10 * c + n
if input[0] == '-':
return -reduce(char_to_int, input[1:], 0)
return reduce(char_to_int, input, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment