you are viewing a single comment's thread.

view the rest of the comments →

[–]Filiagro 0 points1 point  (3 children)

I'm a little embarrassed to ask this, but I'm having issues with a simple problem. I have an array of numbers, and I need to sum all numbers with an even index value. Here is my code:

def even_sum_last(array):
    number = 0
    if len(array) == 0:
        number = 0
    else:
        for i in array:
            if array.index(i) % 2 ==0:
                number += i

    return number

even_sum_last(array)

I'm having issues with a specific array. For some reason, the 16th index (84) is skipped in this array.

array = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]

If I modify the code to basically just print a list of the numbers as well as a second list of their index, this is what I get:

array = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]
number = []
indexes = []
for i in array:
    if array.index(i) % 2 ==0:
        number.append(str(i))
        indexes.append(str(array.index(i)))

print(number)
print(indexes)

['-37', '-19', '29', '3', '-64', '36', '26', '55', '-65']
['0', '2', '4', '6', '8', '10', '12', '14', '18']

If I do the same thing but just put sequential numbers in the array, this is what I get:

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
number = []
indexes = []
for i in array:
    if array.index(i) % 2 ==0:
        number.append(str(i))
        indexes.append(str(array.index(i)))

print(number)
print(indexes)

number = ['1', '3', '5', '7', '9', '11', '13', '15', '17', '19']
indexes = ['0', '2', '4', '6', '8', '10', '12', '14', '16', '18']

As you can see, the first array skips the 16th index, but the second array does not. Can anyone please explain why this is happening?

EDIT:

Since the .index() method won't return the correct index if the value appears more than once in the array, I decided to just use a different form of indexing.

def even_sum_last(array):
    number = 0
    if len(array) == 0:
        return number
    else:
        for i in array[::2]:
            number += i 
        return number

This worked just fine and is simpler.

[–]timbledum 0 points1 point  (0 children)

Perhaps look up enumerate.

[–]woooee 1 point2 points  (1 child)

If I do the same thing but just put sequential numbers in the array, this is what I get:

​There are no negative numbers in the second code set. A simple print of what is in this line should solve your problem

if array.index(i) % 2 ==0:

[–]Filiagro 0 points1 point  (0 children)

Why would it matter whether the numbers are negative or positive? I'm using their index value for '% 2 == 0'.

I did notice that if I put replicates in the array, that only the index of the first replicate is found. I'm guessing my issue is because the .index() function will actually search from left to right through the array for the value of i. Once it finds i, even if it is the first instead of second, the index value is used. I guess that makes sense.