I have been working on a function in Python that finds the sum of all the elements in an array from their respective indices to the start of the array.
Example:
Input: [2,14,17,36]
Output: [2, 14+2, 17+14+2, 36+17+14+2]
This is the code.
import matplotlib.pyplot as plt
import numpy as np
arr = []
a = np.array([2, 0, 0, 4, 0, 1, 0, 4, 5, 5])
def rolling_sum(x):
total = 0
values = []
for i,j in enumerate(x):
total = total+j
values.append(total)
if total <= 2000000000:
arr.append(values)
return rolling_sum(values)
else:
return values
rolling_sum(a)
for i in arr:
plt.plot(i)
Inspecting the arr
variable reveals that there are negative numbers in it and it is even shown clearly from the graph. Please why is this so?
This is how my graph looks like
image