Drill

ProblemsGo › reporting

Running balance down a statement

easyreportingArraysPrefix sumsGo

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

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

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func runningTotal(amounts []int) []int {
	
}

Worked examples

CallResult
runningTotal([]int{1, 2, 3})[]int{1, 3, 6}
runningTotal([]int{5, -5, 5})[]int{5, 0, 5}
runningTotal([]int{7})[]int{7}
runningTotal([]int{})[]int{}

Hint

Carry one accumulator down the list and push it after each step.

Reference solution in Go
func runningTotal(amounts []int) []int {
	sum := 0
	result := []int{}
	for _, a := range amounts {
		sum += a
		result = append(result, sum)
	}
	return result
}

The same problem in another language

More reporting problems in Go