You may remember Smoothing the Weather task, where we first met the idea of calculating "moving average": really, every day we find average of three last days.
Let's try somewhat advanced version of this process. Suppose some value is measured (perhaps, water flow in hydro plant or number of error
requests) but is not reported in equal intervals. Rather data came with timestemps. We want to collect them carefully and sometimes, when asked,
to produce average over the last minute (60
seconds).
Input data are some amount of lines, each containing two numbers timestamp
and value
. Timestamp is in milliseconds, from the start of
the experiment. The last line will have -1 -1
. At some lines value
is not a number but question mark - this means we want to tell average
for last minute at this moment.
Answer: should give several numbers (average values), separated by space - corresponding to moments where average was requested with the question mark in input data. Round values to the nearest integer.
Example:
input:
0 500
1000 600
10000 ?
20000 500
30000 ?
70000 ?
-1 -1
answer:
550 533 500
Here, the average was checked at 10.0 seconds, 30.0 seconds, and 70.0 seconds. Obviously the first check included two first reported values, the second all three reported values, but the last only single (last) reported value, as others were already forgotten.
You may also try Moving Average on Metrics - the full version of this exercise. It wants us to calculate several average values over several metrics simultaneously - and also is somewhat time-sensitive.