Range totals
Inputs
values is a list of 1 to 12 whole numbers, each from -9 to 9. queries is a list of 0 to 6 pairs [start, end], where 0 <= start <= end < len(values).
Your tasks
- Build a list
prefixof lengthlen(values) + 1, whereprefix[0]is 0 and each next item adds one more number. - Use a loop over
queries. The total for[start, end]isprefix[end + 1] - prefix[start]. Add it toanswers. - Complete
rangeTotalsso it returnsanswers. Return it, do not print it.
Example
With values = [2, 4, -1, 3, 5] and queries = [[0, 2], [1, 4], [3, 3]], rangeTotals returns [5, 11, 3].
Rules
- Both ends of a query are included.
- Each query should take one subtraction, not a new loop over the range.
Test data
TEST DATA 1: values is [2, 4, -1, 3, 5], queries is [[0, 2], [1, 4], [3, 3]]
TEST DATA 2: values is [7], queries is []
Practice focus
Prefix sums, counting, binary search, best stretch and sorting, all written as plain loops. This is where loops turn into named algorithms. You have already written the loops.