Created
March 21, 2013 20:14
-
-
Save angusb/5216299 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
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