Exercise 71

Write a Python algorithm that transforms a list of integers L = [n1, n2, n3, ..., np] by adding 1 to the first element, 2 to the second element, 3 to the third element, ..., p to the pth element. The algorithm must return at the end the list

[n1 + 1, n2 + 2, n3 + 3, ..., np + p]

.

Solution

#coding: utf-8
def translatedList (L):
# initialization of the translated list
TList = []

for i in range (0, len (L)):
TList.append (L [i] + i + 1)
return TList

# Example
L = [3, 1, 7, 11, 2]
print (translatedList (L)) # the output: [4, 3, 10, 15, 7]
Younes Derfoufi
my-courses.net

Leave a Reply