I have a task "You are given an array of integers. You should find the sum of the integers with even indexes (0th, 2nd, 4th...). Then multiply this summed number and the final element of the array together. Don't forget that the first element has an index of 0.
For an empty array, the result will always be 0 (zero).
Input: A list of integers.
Output: The number as an integer."
My solution is:
def checkio(array: list) -> int:
"""
sums even-indexes elements and multiply at the last
"""
summary = 0
if len(array) > 0:
for i in array:
if array.index(i) % 2 == 0:
summary = i
print('sum = ' str(summary) ' index = ' str(array.index(i)))
res = summary * array[-1]
return res
else:
return 0
And when it doesnt work correct, when i print indexes to see whats wrong, i got that some index is missing (in this example 16th)
print(checkio([-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]))
Output:
sum = -37 index = 0
sum = -56 index = 2
sum = -27 index = 4
sum = -24 index = 6
sum = -88 index = 8
sum = -52 index = 10
sum = -26 index = 12
sum = 29 index = 14
sum = -36 index = 18
-1476
What is the reason?
CodePudding user response:
The issue is that array.index()
will return the first index of the value
You have a 84
at position 9, then when you call it on the second 84
, the vall is the same array.index(84)
so result is same : 9
and it won't count it
Use enumarate
that make an iterator with both position and value
def checkio(array: list) -> int:
"""
sums even-indexes elements and multiply at the last
"""
summary = 0
if array: # true is non-empty
for i, value in enumerate(array):
if i % 2 == 0:
summary = value
print('sum =', summary,'index =', i)
res = summary * array[-1]
return res
return 0
CodePudding user response:
As mentioned in the answer by @azro, the problem with your code has to do with array.index()
always returning the first index of a given element in your list, so that it will return undesired results for duplicates.
enumerate()
is one way to go, but there's still room for some optimization here. The following bit is verbose, since it will loop over all the elements, while we already know we only need elem 0, 2, etc.
for i, value in enumerate(array):
if i % 2 == 0:
We could just write:
array = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]
def checkio1(array: list) -> int:
"""
sums even-indexes elements and multiply at the last
"""
summary = 0
if array: # true is non-empty
for i in range(0,len(array),2): # 2 is the step, so we enter elem 0, 2, etc.
summary = array[i]
res = summary * array[-1]
return res
return 0
print(checkio1(array))
1968
Let me share two alternative methods as well.
- using
numpy
:
import numpy as np
def checkio2(array: list) -> int:
"""
sums even-indexes elements and multiply at the last
"""
if not array:
return 0
arr = np.array(array)
return arr[np.arange(0,len(arr),2)].sum()*arr[-1]
print(checkio2(array))
1968
print(checkio2([]))
0
- using
itemgetter
from theoperator
module:
from operator import itemgetter
def checkio3(array: list) -> int:
"""
sums even-indexes elements and multiply at the last
"""
if not array:
return 0
return sum(itemgetter(*range(0,len(array),2))(array))*array[-1]
print(checkio3(array))
1968
print(checkio3([]))
0
CodePudding user response:
Here is my attempt, just for fun reference. All previous posts work great.
I don't like to use array here, since it has different meaning in Python.
def checkio(nums: list) -> int:
"""
sums even-indexes elements and multiply at the last
"""
if not nums: # not empty
return sum(nums[0::2]) * nums[-1] # . just rely on range slicing here & save some modulo computations later...
else:
return 0