Created
May 25, 2020 16:45
-
-
Save theoctober19th/ac19e6ae9e751d104e8c10a3e51c8d5a to your computer and use it in GitHub Desktop.
Given: 1 -> 2 -> 3 + 4 -> 5 -> 6. Expected output: 5 -> 7 -> 9
This file contains hidden or 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
def add(li1, li2): | |
sum = [] | |
while li1 and li2: | |
sum.append(li1.pop(0) + li2.pop(0)) | |
while li1: | |
sum.append(li1.pop(0)) | |
while li2: | |
sum.append(li2.pop(0)) | |
return sum | |
string = '(1 -> 2 -> 3) + (3 -> 4 -> 5)' | |
sums = [] | |
for term in string.split('+'): | |
term = term.replace('(', '') | |
term = term.replace(')', '') | |
nums = [] | |
for num in [s.strip() for s in term.split('->')]: | |
nums.append(int(num)) | |
sums = add(sums, nums) | |
sums = map(str, sums) | |
sums = ' -> '.join(sums) | |
print(sums) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment