Page 1 of 1

Python Code: Problem – numlib.cumsum() - Define a function numlib.cumsum which accepts a list of numbers (i.e., either f

Posted: Sun May 15, 2022 8:28 am
by answerhappygod
Python Code:
Problem – numlib.cumsum() - Define a function numlib.cumsum
which accepts a list of numbers (i.e., either floats or ints)and
returns a new list that contains the cumulative sums (also known as
the prefix sums) of the input list. Clearly, the returned list
should have length equal to the input list (this includes zero
length, of course). Furthermore, you should not modify the input
list in any way. Formally, for any sequence x0,x1,...,xN−1, the
corresponding cumulative sum sequence cs0,cs1,...,csN−1 is defined
by cs0 := x0, cs1 := x0 + x1, ..., csN−1 := x0 + x1 + ···+ xN−1 or,
generally stated, csi := x0 + x1 + ···+ xi, for all 0 ≤i <
N.
Problem – numlib.running_mean() - Define a function
numlib.running_mean, which accepts a list of numbers as an
argument, and returns a new list containing the corresponding
sequence of “running means”. More specifically, for any sequence
x0,x1,...,xN−1, the corresponding running mean sequence
μ0,μ1,...,μN−1 is defined by μ0 := x0 1 , μ1 := x0 + x1 2 , ...,
μN−1 := x0 + x1 + ···+ xN−1 N or, generally stated, μi := x0 + x1 +
···+ xi i+ 1 , for all 0 ≤i < N.
Clearly, the returned list should contain floats, and should be of
the same length as the input argument list. As always, your code
should not modify the input list in any way.
Hint: If you reuse numlib.cumsum†, this should be easy, since μi
= csi/(i + 1) for all 0 ≤ i < N. Just consider iterating over
the indices i (in the range 0 ≤i < N), rather than directly over
the values csi—the reason is simple: you need both i and csi, and
you can always look up csi if you have i (but not the other way
around).