0

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?

  • What is the purpose of the `|` symbol in the first `if` statement? – John Gordon Sep 01 '21 at 02:06
  • 1
    the first if, pls use `or`, not `|`. They are different. – Trock Sep 01 '21 at 02:07
  • @Trock Oh is it, I was thinking it should be an or. But I guess it should have either been a 'or' (as you suggested) or a '||' – roshan shetty Sep 01 '21 at 02:11
  • @JohnGordon It's supposed to be an or, but I may have made a mistake. – roshan shetty Sep 01 '21 at 02:11
  • @Trock I am still curious why using a '|' messed up the code so much? What does the pipe do in Python? – roshan shetty Sep 01 '21 at 02:14
  • @roshanshetty please learn it [python_operators](https://www.w3schools.com/python/python_operators.asp) – Trock Sep 01 '21 at 02:21
  • @Trock Thanks. Since it's a binary operator, did it change all variables to 1 (binary) as an operation? Why did it affect from_l_count though? – roshan shetty Sep 01 '21 at 02:25
  • @roshanshetty for your code, the `else` mean `sum_from_f_elem == sum_from_l_elem`, need `from_f_count += 1` and `from_l_count -= 1`. And don'n need ` or from_f_count == 0` – Trock Sep 01 '21 at 02:28
  • this question is poorly constructed. don't beat yourself up. it doesnt matter if a Fortune 500 company (apple) asked this question for their interview. a better company would ask questions that actually make sense........... for example, this question doesn't define what happens to the middle element when you split the list – Joshua Sep 01 '21 at 02:51

1 Answers1

0

Your code is complicated by keeping track of 2 indices when you really only need to keep track of the midpoint.

Also, you are using a bitwise operator | rather than a boolean operator or as the previous commenters pointed out.

As a hint for completing this question a little bit more cleanly, you should look into the sum method and slice notation

W-B
  • 850
  • 5
  • 16