[Solved] Write a Python function, the syntax is ReplaceMe

Write a Python function, the syntax is ReplaceMe(input)

Where input is a list of numbers and this function would replace each element with its running sum. The following example would give some idea.

Let 1,2,3,4 is the list output would be 1,3,6,10. Do not use any library functions, use loops and other basic structures.

def solve(nums):
   n = len(nums)
   rs = [nums[0]]
   #sauravhathi
   for i in range(1, n):
      nums[i] += nums[i-1]
      #sauravhathi
      rs.append(nums[i])
   return rs

nums = [1, 2, 3, 4]
print(solve(nums))

Output:

[1, 3, 6, 10]

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *