I attempted solving this problem:
"Given a list of integers, find the index at which the sum of the left half of the list is equal to the right half.
If there is no index where this condition is satisfied return -1"
I wrote a function which I expected to work.
```python
def solution(nums=[1, 7, 3, 5, 6]):
# We can compute a sum from first element and a sum from last
# element.
sum_from_f_elem = 0
sum_from_l_elem = 0
from_f_count = 0
from_l_count = len(nums) - 1 # 4
# Counter added as code was going into an infinite loop otherwise
cnt = 0
while from_f_count <= from_l_count: # 2 <= 2
# Trock's comment helped in getting the code run. Changing the | to or worked. I still don't know what the | does below to mess up the code so much
if (sum_from_f_elem < sum_from_l_elem) | from_f_count == 0:
sum_from_f_elem += nums[from_f_count] # 11
from_f_count += 1 # 3
print("Sum from first element: {0}, first element count: {1} ".format(
sum_from_f_elem, from_f_count))
elif sum_from_f_elem > sum_from_l_elem:
sum_from_l_elem += nums[from_l_count] # 11
from_l_count -= 1 # 2
print("Sum from last element: {0}, last element count: {1} ".format(
sum_from_l_elem, from_l_count))
else:
print("Sum from first element: {0}, first element count: {1}, Sum from last element: {0}, last element count: {1} ".format(
sum_from_f_elem, from_f_count, sum_from_l_elem, from_l_count))
cnt += 1
print("Count = ", cnt)
if cnt >= 100:
break
if sum_from_f_elem == sum_from_l_elem:
return from_f_count
else:
return -1
```
The function call - solution(nums=[1, 7, 3, 5, 6]) returns the following results though:
Sum from first element: 1, first element count: 1 Count = 1
Sum from last element: 6, last element count: 3 Count = 2
Sum from first element: 1, first element count: 1, Sum from last element: 1, last element count: 1 Count = 3
Sum from first element: 1, first element count: 1, Sum from last element: 1, last element count: 1 Count = 4
Sum from first element: 1, first element count: 1, Sum from last element: 1, last element count: 1 Count = 5
Sum from first element: 1, first element count: 1, Sum from last element: 1, last element count: 1
.. and so on till it reaches 100
Count = 99
Sum from first element: 1, first element count: 1, Sum from last element: 1, last element count: 1 Count = 100
What am I doing wrong?