Calculate the bounded cumulative sum of a vector
CJam, 16 15 bytes
l~f{\T++$1=:T}`
Try it online
This takes the list as first argument, and the pair of upper/lower limit as a second 2-element list. Example input:
[1 4 3 -10 3 2 2 5 -4] [6 -2]
The latest version saves 1 byte by sorting the 3 values, and taking the middle value, instead of using a max and min operation. This was also used in Jakube's solution, as well as suggested by Martin.
Explanation:
l~ Get and parse input. This leaves the value and bounds lists on the stack.
f{ Apply block with value (the bounds list).
\ Swap new value to top.
T Get previous value from variable T (which is default initialized to 0).
+ Add new value and previous value.
+ Append new value to bounds list, producing a 3 value list.
$ Sort it...
1= And take the middle value.
:T Store in variable T for next iteration.
} End of apply loop.
` Convert list to string.
Pyth, 14 bytes
t.u@S+Q+NY1vwZ
Try it online: Demonstration or Test Suite
Explanation
t.u@S+Q+NY1vwZ implicit: Q = first input list [upper_lim, lower_lim]
.u vwZ for each number Y in the next input list, update N = 0 with:
+NY N + Y
+Q append this to Q
S sort this list
@ 1 take the middle element
.u returns a list with all intermediate values of N
t remove the first value, print the rest
JavaScript (ES6), 43 bytes
(l,u,v,p=0)=>v.map(c=>p=(p+=c)<l?l:p>u?u:p)
Defines an anonymous function that takes input in the format lower bound, upper bound, vector (as JS Array)
. I don't know if it could be any shorter, but I'll try. Suggestions welcome!