Sure, here’s a program in Python to compute the cumulative sum of a given list:Sure, here’s a program in Python to compute the cumulative sum of a given list:
def cumulative_sum(lst): """ Returns a new list where each element is the cumulative sum of the input list up to that point. """ cum_sum = [] current_sum = 0 for num in lst: current_sum += num cum_sum.append(current_sum) return cum_sum
You can use this function as follows:
>>> lst = [1, 2, 3, 4, 5] >>> cumulative_sum(lst) [1, 3, 6, 10, 15]
In this example, the cumulative sum of [1, 2, 3, 4, 5]
is [1, 3, 6, 10, 15]
. The first element of the output list is the same as the first element of the input list, the second element of the output list is the sum of the first two elements of the input list, and so on.