Drill

ProblemsPython › reporting

Running balance down a statement

easyreportingPython

An account statement shows the balance after every movement, not just the final figure.

running_total(amounts: list<int>) → list<int>

Solve it in the editor →

Where you start

def running_total(amounts: list[int]) -> list[int]:
    

Worked examples

CallResult
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

The same problem in another language

More reporting problems in Python