Fill the blank
Fill the blank.
def balance(w: list[int]) -> int:
total, left = sum(w), 0
for i, x in enumerate(w):
if ____:
return i
left += x
return -1
print(balance([1, 7, 3, 6, 5, 6]))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A left == total - left - x
Right: the right side is everything except the left side and the pivot itself.
For [1, 7, 3, 6, 5, 6] the left side at index 3 is 11 and the right side is 28 - 11 - 6 = 11.
Why not the others?
- B left == total - left. This counts the pivot on the right side.
- C left + x == total. That compares the left side plus the pivot with the whole total, which only holds when nothing is on the right, so it prints 5.