import sys def _debug(*args): print(*args, file=sys.stderr) def sum(left: str, right: str) -> str: def gen(): rleft = reversed(left) rright = reversed(right) carry = 0 while True: left_digit, right_digit = next(rleft, None), next(rright, None) sum = int(left_digit or 0) + int(right_digit or 0) + carry if sum >= 10: yield sum - 10 carry = 1 elif left_digit is None and right_digit is None: if sum: yield sum return else: yield sum carry = 0 return ''.join(reversed([str(x) for x in gen()])) def main(): a = sys.stdin.readline().strip() b = sys.stdin.readline().strip() r = sum(a, b) sys.stdout.write(str(r)) sys.stdout.flush() if __name__ == '__main__': main()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import sys def _debug(*args): print(*args, file=sys.stderr) def sum(left: str, right: str) -> str: def gen(): rleft = reversed(left) rright = reversed(right) carry = 0 while True: left_digit, right_digit = next(rleft, None), next(rright, None) sum = int(left_digit or 0) + int(right_digit or 0) + carry if sum >= 10: yield sum - 10 carry = 1 elif left_digit is None and right_digit is None: if sum: yield sum return else: yield sum carry = 0 return ''.join(reversed([str(x) for x in gen()])) def main(): a = sys.stdin.readline().strip() b = sys.stdin.readline().strip() r = sum(a, b) sys.stdout.write(str(r)) sys.stdout.flush() if __name__ == '__main__': main() |