Skip to content

Instantly share code, notes, and snippets.

@artburkart
Last active August 29, 2015 14:08
Show Gist options
  • Save artburkart/aa92a6596c61ba164ac9 to your computer and use it in GitHub Desktop.
Save artburkart/aa92a6596c61ba164ac9 to your computer and use it in GitHub Desktop.
add string numbers
def add_string_nums(s1, s2):
if s1 is None or s2 is None:
raise AttributeError()
val = 0
rev_s1 = s1[::-1]
rev_s2 = s2[::-1]
for i in range(len(s1)):
curr = ord(rev_s1[i])
if curr >= 48 and curr <= 57:
val += (curr - 48) * 10 ** i
else:
raise AttributeError()
for i in range(len(s2)):
curr = ord(rev_s2[i])
if curr >= 48 and curr <= 57:
val += (curr - 48) * 10 ** i
else:
raise AttributeError()
return val
print(add_string_nums("1002", "4001"))
print(add_string_nums("10", "1"))
print(add_string_nums("1008", "12"))
print(add_string_nums("9999", "9999"))
print(add_string_nums("11111", "111111"))
print(add_string_nums("0", "1"))
print(add_string_nums("1000000000", "1"))
try:
print(add_string_nums("a", "1"))
except AttributeError:
print("That's not gonna work")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment