Streaming Average

When data arrives as a stream, it is often useful to smooth it by averaging only the most recent values. Given a window size size and a list stream, compute the average after each new number arrives. Each average should use the last at most size values. Return the averages in the same order as the stream.

Примеры

Ввод
3, [1, 10, 3, 5]
Вывод
[1, 5.5, 4.666666666666667, 6]
Ввод
1, [4, 5, 6]
Вывод
[4, 5, 6]
Ввод
2, [2, 2, 2, 2]
Вывод
[2, 2, 2, 2]

Нужно реализовать

def moving_average(size, stream):

Теги

arraysliding-windowmath
PostgreSQLv16

Hier verschijnt het resultaat van je code

Visible tests
1
Input
3, [1,10,3,5]
Expected[1,5.5,4.666666666666667,6]
2
Input
1, [4,5,6]
Expected[4,5,6]
3
Input
2, [2,2,2,2]
Expected[2,2,2,2]
4
Input
5, []
Expected[]
5
Input
2, [10]
Expected[10]
6
Input
2, []
Expected[]
Focus radio
Paused · SomaFM · Fluid