add strings

🏠
 1 from string import digits
 2 
 3 class Solution:
 4     def addStrings(self, num1: str, num2: str) -> str:
 5       output = ''
 6       ai, bi, carry = 0, 0, 0
 7       while ai < len(num1) or bi < len(num2) or carry:
 8         n1v = digits.index(num1[~ai]) if ai < len(num1) else 0
 9         n2v = digits.index(num2[~bi]) if bi < len(num2) else 0
10         carry, v = divmod(n1v + n2v + carry, 10)
11         output = digits[v] + output
12         ai, bi = ai+1, bi+1
13       return output
14 
15 assert Solution().addStrings('123', '458') == '581'