Binary Reduce a List By Addition With a Right Bias
Python 2, 40 bytes
Takes as input a list \$ l \$, and outputs the two sums in reverse order ([right, left]
).
lambda l:[sum(l.pop()for x in l),sum(l)]
Explanation
In sum(l.pop()for x in l)
, we pop the last element, in each iteration, and sum the popped elements. Surprisingly, the loop only runs \$ \lceil{\frac{|l|}{2}}\rceil \$ times, since for every element we iterate from the left, we are removing an element from the right, resulting in the loop terminating somewhere in the middle. Therefore it gives us the sum of the right portion of the list. The sum of the remaining elements make up the left portion.
Try it online!
Python 3.8, 41 bytes
lambda l:[t:=sum(l[:len(l)//2]),sum(l)-t]
Try it online!
Straightforward solution. Takes in a list, and returns the list of left and right sum.
Interesting idea that didn't go anywhere :(
Python 3, 45 bytes
lambda l,t=1j:l>[]and l[-1]+t*f(l[-2::-1],-t)
Try it online!
Returns a+bj
where a, b
is the right and left sum respectively.
Haskell, 44 43 bytes
f x=[sum$y(div(length x)2)x|y<-[take,drop]]
Try it online!
Based on Steven Fontanella's answer with some non-trivial modification.