easy +10 pts

Cumulative Sum Vectorized

Compute the cumulative sum of a list using a loop or Python's built-ins.

Write a function `cumulative_sum(arr)` that takes a list of numbers (integers or floats) and returns a new list where each element at index i is the sum of all elements from index 0 to i inclusive. You may use a simple loop or any Python built-in function. Do not use NumPy or any external libraries. If the input list is empty, return an empty list.

Constraints

Input is a list of numbers (integers or floats). Length between 0 and 10^6. The output must have the same length as the input. Time complexity O(n), memory O(n) for the output.

Example

>>> cumulative_sum([1, 2, 3, 4])
[1, 3, 6, 10]
>>> cumulative_sum([1, 2, 3])
[1, 3, 6]
>>> cumulative_sum([])
[]
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Initialize an empty result list and a running total variable.
Iterate through each number and add it to the running total, then append the total to the result.
Alternatively, use itertools.accumulate for a one-liner.
For an empty list, the loop naturally produces an empty list.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.