Python-courses-python-exercises-python-exception-python-pip

Exercise 62

Given a list of integers L, write an algorithm in Python which multiplies the elements of L of even index by 2 and those of odd index by 3. Example if L = [3, 2, 7, 11, 5, 3], the algorithm returns the list [6, 6, 14, 33, 10, 9]

Solution

def mult (L):
# initialize the searched list
listMult = []
for n in L:
if n% 2 == 0:
listMult.append (2 * n)
else:
listMult.append (3 * n)
return listMult


# Example
L = [3, 2, 7, 11, 5, 3]
print (mult (L)) # prints [9, 4, 21, 33, 15, 9]
Younes Derfoufi
my-courses.net

Leave a Reply