Positive partial sums X54583


Statement
 

pdf   zip

thehtml

The partial sums of a list of integers v are:

v[0]
v[0]+v[1]
v[0]+v[1]+v[2]
...
v[0]+v[1]+v[2]+...+v[len(v)-1]

Design a function @sumes_parcials_pos(v)@ that, given a list of integers v, returns the list containing the positive partial sums of the list v, that is, those that are greater than zero.

For example, if v is [0, 3, -4, -5, 7], then the partial sums are

0
0+3 == 3
0+3+(-4) == -1
0+3+(-4)+(-5) == -6
0+3+(-4)+(-5)+7 == 1

and, therefore, the list with the two positive partial sums must be returned

[3, 1] == [0+3, 0+3+(-4)+(-5)+7]

DON’T use the Python function sum(v[i:j]).

Sample session
>>> sumes_parcials_pos([6, 3, -2, -5, 7])
[6, 9, 7, 2, 9]
>>> sumes_parcials_pos([0, 3, -4, -5, 7])
[3, 1]
>>> sumes_parcials_pos([])
[]
>>> sumes_parcials_pos([0, -1, -4, -2])
[]
Information
Author
InfBesos
Language
English
Translator
Original language
Catalan
Other languages
Catalan Spanish
Official solutions
Python
User solutions
Python