Can't think of a fitting title for my question.
So anyway, I've been looking for an algorithm to find out if a list is balanced or not and I've came across this question: Algorithm for finding if an array is balanced
One of the answers is spot-on to what I need to accomplish. I would however, like to understand what happens if I change line 2 from the below code.
def balanced(numbers):
left_total, right_total = 0, sum(numbers)
for pivot, value in enumerate(numbers):
if left_total == right_total:
return pivot
left_total += value
right_total -= value
return None
I would like to know why it throws a TypeError: 'int' object is not iterable if I do this to line 2:
left_total = 0
right_total = 0
sum(numbers)
Hope someone can help me understand. Thanks!