Running balance down a statement
An account statement shows the balance after every movement, not just the final figure.
- The result is the same length as the input.
- Each entry is the sum of everything up to and including that position.
running_total(amounts: list<int>) → list<int>
Where you start
def running_total(amounts: list[int]) -> list[int]:
Worked examples
| Call | Result |
|---|---|
running_total([1, 2, 3]) | [1, 3, 6] |
running_total([5, -5, 5]) | [5, 0, 5] |
running_total([7]) | [7] |
running_total([]) | [] |
Hint
Carry one accumulator down the list and push it after each step.
Reference solution in Python
def running_total(amounts: list[int]) -> list[int]:
total = 0
result = []
for a in amounts:
total += a
result.append(total)
return result