Drill

ProblemsJava › patterns

The average so far, after every reading

easypatternsPrefix sumsArraysMathJava

A quality chart plots not each reading but the average of everything measured up to that point, so a late wobble does not swing the line.

runningAverage(readings: list<int>) → list<int>

Java 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

List<Integer> runningAverage(List<Integer> readings) {
    
}

Worked examples

CallResult
runningAverage(Main.<Integer>ls(10, 20, 30))Main.<Integer>ls(10, 15, 20)
runningAverage(Main.<Integer>ls(1, 2))Main.<Integer>ls(1, 1)
runningAverage(Main.<Integer>ls(5))Main.<Integer>ls(5)
runningAverage(Main.<Integer>ls())Main.<Integer>ls()

Hint

Carry the running total and divide by how many readings you have seen. Do not re-add the list each step.

Reference solution in Java
List<Integer> runningAverage(List<Integer> readings) {
    List<Integer> out = new ArrayList<>();
    int total = 0;
    for (int i = 0; i < readings.size(); i++) {
        total += readings.get(i);
        out.add(total / (i + 1));
    }
    return out;
}

The same problem in another language

More patterns problems in Java