#C13825. Moving Average Calculator
Moving Average Calculator
Moving Average Calculator
You are given a time series of prices for consecutive days and an integer window size k. Your task is to compute the simple moving average. For each day i (1-indexed), if there are fewer than k prices available (i.e. when i < k), output None
. Otherwise, output the average of the last k prices rounded to one decimal place.
The moving average for a window of k consecutive days is computed using the formula:
$$\text{avg} = \frac{price_{i-k+1} + price_{i-k+2} + \cdots + price_{i}}{k}$$
Note that the input is provided via standard input (stdin) and the output should be written to standard output (stdout).
inputFormat
The first line contains two space-separated integers: n
(the number of days) and k
(the moving average window size). The second line contains n
space-separated numbers representing the prices for each consecutive day.
outputFormat
Output n
space-separated values. For the days where the moving average cannot be computed (i.e. when there are fewer than k
prices), output None
. Otherwise, output the moving average rounded to one decimal place.
5 3
1 2 3 4 5
None None 2.0 3.0 4.0