Last active
March 25, 2020 12:07
-
-
Save smallyunet/2386960da7348ca67bd33445d8933634 to your computer and use it in GitHub Desktop.
get sum by two string
This file contains 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
""" | |
@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