Drill

ProblemsC# › patterns

The average so far, after every reading

easypatternsPrefix sumsArraysMathC#

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>

C# 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

public List<int> RunningAverage(List<int> readings) {
    
}

Worked examples

CallResult
RunningAverage(new List<int> { 10, 20, 30 })new List<int> { 10, 15, 20 }
RunningAverage(new List<int> { 1, 2 })new List<int> { 1, 1 }
RunningAverage(new List<int> { 5 })new List<int> { 5 }
RunningAverage(new List<int> { })new List<int> { }

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 C#
public List<int> RunningAverage(List<int> readings) {
    var outList = new List<int>();
    int total = 0;
    for (int i = 0; i < readings.Count; i++) {
        total += readings[i];
        outList.Add(total / (i + 1));
    }
    return outList;
}

The same problem in another language

More patterns problems in C#