Answer to Question #197868 in Python for LEROY

Question #197868

Write a program using function called cumulative_sum that takes a list of numbers and returns the cumulative sum; that is, a new list where the nth element is the sum of the first n elements from the original list. For example, the cumulative sum of [1, 2, 3] is [1, 3, 6]. 


1
Expert's answer
2021-05-24T07:42:54-0400
def camulative_sum(lst:list):
    res = list()
    sum = 0
    for item in lst:
        sum += item
        res.append(sum)
    return res

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS