• Index Problem in Elementary Puzzle "even-last"


I encountered a weird bug in one of the checks for this puzzle.

For the array [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41] my function threw an AssertionError, although all other checks went fine. After some digging, I found that something is wrong with the indexes that Python returns. There is a 9 where the index 16 should be.

... Element index: 14 Element value: 55

Element index: 15 Element value: -24

Element index: 9 Element value: 84

Element index: 17 Element value: 49

Element index: 18 Element value: -65 ...

The code I used was:

def checkio(array):
        sums even-indexes elements and multiply at the last
    for element in array:
        print("Element index:", array.index(element))
        print("Element value:", element, "\n")
        summe = sum({element for element in array if array.index(element) % 2 == 0})
        return summe*array[-1]
    except IndexError:
        return 0

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    assert checkio([-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]) == 1968, "41*(-37-19+29+3-64+36+26+55+84-65)"

I would be interested to hear what caused this strange behavior.

Thank you so much.


From: https://py.checkio.org/mission/even-last/solve/

HTTP_USER_AGENT: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0