Exercise 54
Write a Python algorithm as a function which takes as parameter a list of integers L = [n1, n2, n3, ...., np] and which returns the list: [n1, n1 + n2, n1 + n2 + n3, ...., n1 + n2 + ... + np]
Solution
def transformedList (L):
# initialization of the transformed list
lTransformed = []
# item initialization
item = 0
for i in range (0, len (L)):
item = item + L [i]
lTransformed.append (item)
return lTransformed
# Example
L = [2, 1, 5, 3, 4]
print (transformedList (L)) # prints [2, 3, 8, 11, 15]
Younes Derfoufi
my-courses.net
my-courses.net