arrays - Trouble with Basic Python exercise involving lists and indexes -


working on checkio exercises stuck here. need design function that'll find sum of elements indexes (0th, 2nd, 4th...) multiply summed number , final element of array together. input array, output number. oh, , empty array, result has zero.

def checkio(array):     sum = 0     if len(array) == 0:          return 0     else:         in array:                 if array.index(i) % 2 == 0:                  sum = sum +         final = sum*(array[len(array)-1])     return final 

for instance, array [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41], function returning -1476 when should giving out 1968.

here working program made.

def checkio(array):     listsum = 0      if array:          in range(0, len(array), 2):             listsum += array[i]          finalvalue = listsum * array[-1]          return finalvalue     else:         return 0 

first, checks see if array has values. can this: if array:. if array empty, return 0 wanted.

now, checks every other element in array: range(0, len(array), 2): means value of i start @ 0, continue length of array, , count twos.

the sums added here: listsum += array[i]. takes variable listsum , adds value of number @ index i in array it. += operator shorthand listsum = listsum + array[i].

the last part of function, takes listsum variable, , multiplies array[-1] gets last value in array , finnaly returns it.

when ran example array above, returned 1968 should.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -