list - Python code for sum with condition -
the task following: sum list elements indexes , multiply result last list's elemet. have oneliner solution code in python.
array = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41] print sum(i in array if array.index(i) % 2 == 0)*array[-1] if array != [] else 0
my result -1476 ( calculation is: 41*(-37-19+29+3-64+36+26+55+84-65) )
the right result 1968.
i can't figure out why code not working correctly in particular case.
there repeated element 84
in list, array.index
not work expected be. also, code has quadratic complexity not required.
to fix code minimum amount of edit, this:
array = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41] print sum(array[i] in range(len(array)) if % 2 == 0)*array[-1] if array != [] else 0
Comments
Post a Comment