2

I'm solving a puzzle that takes in a list, loops through each index and sums the values to the left and to the right of the current index. If it finds an index at which the sum of the values to its left are equal to the sum of the values to its right, then return that index.

For example: [1,2,3,4,3,2,1] If I am at index 3 (value 4), we see that the sum of values of elements to its left and right are equal. (1 + 2 + 3) = (3 + 2 + 1).

However, when the input is a list of negative values, it returns a negative index. This is my code:

def get_equal_sum(list):
    for i in list:
        if sum(list[:i]) == sum(list[i+1:]):
            print i

list = [1,2,3,4,3,2,1]
get_equal_sum(list)
>>> 3

list = [-1,-2,-3,-4,-3,-2,-1]
get_equal_sum(list)
>>> -4

Why is it returning -4 and not 3?

Thank you!

tbd_
  • 1,058
  • 1
  • 16
  • 39

3 Answers3

4

When you do

for i in list:
    if sum(list[:i]) == sum(list[i+1:]):

i is not the index of the list but the value. The fact that it doesn't crash with IndexError when you feed the slices with negative values is because negative indexing is supported in python (indexes from the end of the list) as long as absolute value is in range (which is the case here). That's probably why you missed that.

If you want indexes you have to use enumerate for instance:

for i,_ in enumerate(l):
    if sum(l[:i]) == sum(l[i+1:]):

(and change list to l because list is the list type)

Note the i,_ notation to unpack index/value and discard the value which isn't needed here. You could also go with the classical:

for i in range(len(l)):
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • then print `_` for when the `if` statement is `True`. – foxyblue Apr 20 '17 at 16:42
  • @GiantsLoveDeathMetal I see your point, but I think OP wants to print index as well (hence the `i` name). The values are only used when testing sublists for equality. I wouldn't have used `_` if I wanted to use the variable. – Jean-François Fabre Apr 20 '17 at 16:44
  • yeah OP wants to `return i`, I got that wrong in my comment – foxyblue Apr 20 '17 at 16:48
  • @Jean-FrançoisFabre got it working! I see now why I was returning value instead of index. Thank you! – tbd_ Apr 20 '17 at 20:17
3
for i in list:
    ...

This goes through the values of list, not the index (hence you why you get -4), to go through index you must use xrange (or range for python 3) over the len(list)

for i in xrange(len(list)):
    ...

or use enumerate(list). enumerate returns a tuple of the index value pair on any iteratable object, so you would index over a list like this:

for index, value in enumerate(list):
    ...

In this situation index is likely your i. Additionally, you do not want to shadow names already used by python itself (ie list or max, or min abs etc...) this will overwrite those variables to be what you assign them to in your program. For example, list is no longer a function that can create a list from list(iterable), but is now what ever you assign it to, you can no longer use it the default python way. Here is a list of python 2.7 built function in names

Community
  • 1
  • 1
Krupip
  • 4,404
  • 2
  • 32
  • 54
0

Because -4 is at index -4. You can have negative indices in Python with index -1 being the last element in a list.

DrBwts
  • 3,470
  • 6
  • 38
  • 62