Skip to content

Instantly share code, notes, and snippets.

@smallyunet
Last active March 25, 2020 12:07
Show Gist options
  • Save smallyunet/2386960da7348ca67bd33445d8933634 to your computer and use it in GitHub Desktop.
Save smallyunet/2386960da7348ca67bd33445d8933634 to your computer and use it in GitHub Desktop.
get sum by two string
"""
@desc get sum by two string
@param string s1
@param string s2
@return string
"""
def sumByString(s1, s2):
s1 = s1.rjust(len(s2), '0')
s2 = s2.rjust(len(s1), '0')
a = list(s1[::-1])
b = list(s2[::-1])
s = ''
f = '0'
for i in range(len(a)):
c = int(a[i]) + int(b[i]) + int(f)
s += str(c % 10)
f = int(c / 10)
if int(f) > 0:
s += str(f)
return s[::-1]
# r = sumByString('1234243243245','42342342456')
# print(r)
# 1276585585701
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment