Lesson 6
Dealing with Sequences of Objects
Chapter 5: Applied problem solving
Cumulative sum
Now, write a function compute_cumulative_sum()
that takes a list of integers as input, and returns a list containing the cumulative sum at each index.
For example, if the input is [1, 5, 3, 2, 1]
, the function should return [1, 6, 9, 11, 12]
(that is, [1, 1+5, 1+5+3, 1+5+3+2, 1+5+3+2+1]
).
As usual, write test cases to ensure your function works as expected.
Sample inputs and outputs
Example 1
- Input:
[5, 2, 1, 7, 3, 8]
- Output:
[5, 7, 8, 15, 18, 26]
Example 2
- Input:
[3, 5, 4, 1, 2]
- Output:
[3, 8, 12, 13, 15]
Example 3
- Input:
[5]
- Output:
[5]
Example 4
- Input:
[]
- Output:
[]