Skip to content

Instantly share code, notes, and snippets.

@theoctober19th
Created May 25, 2020 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theoctober19th/ac19e6ae9e751d104e8c10a3e51c8d5a to your computer and use it in GitHub Desktop.
Save theoctober19th/ac19e6ae9e751d104e8c10a3e51c8d5a to your computer and use it in GitHub Desktop.
Given: 1 -> 2 -> 3 + 4 -> 5 -> 6. Expected output: 5 -> 7 -> 9
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