Sep-16-2018, 03:17 PM
(This post was last modified: Sep-16-2018, 03:17 PM by Metalman488.)
So I have two lists that are each a large integer that has been split up into nodes. (Example: 123456 => [123,456]) and I need to add two lists like that, so something like [123,456]+[789,654]
This is the code I have so far:
This sort of works, but it doesn't handle carryout and it doesn't work correctly if the lists are of different length. It turns ['001', '234', '567'], '+', ['007', '894', '561'] into [8,1128,1128], the answer is 9128128 so it works but it's not doing 8+1 <= 1 <= 128+1 <= 1 <= 128. I'm not exactly sure how to get it to do carryout.
This is the code I have so far:
def recursive_sum(l1, l2, idx = 0): if idx < min(len(l1), len(l2)): return [int(l1[idx]) + int(l2[idx])] + recursive_sum(l1, l2, idx + 1) else: return []The int() is because the elements are strings from when I was splitting the numbers into nodes.
This sort of works, but it doesn't handle carryout and it doesn't work correctly if the lists are of different length. It turns ['001', '234', '567'], '+', ['007', '894', '561'] into [8,1128,1128], the answer is 9128128 so it works but it's not doing 8+1 <= 1 <= 128+1 <= 1 <= 128. I'm not exactly sure how to get it to do carryout.