A moving average smooths a noisy series so the trend shows through.
What to build
A program that prints the moving average of a series.
Requirements
- Print the average of every full window of k consecutive values, in order, on one line, each to two decimals and separated by spaces.
Input
Line 1: the window k. Line 2: the values separated by spaces.
Output
One line.
Constraints
1 ≤ k ≤ the number of values ≤ 1000.
Example
Input:
3
10 20 30 40 50
Output:
20.00 30.00 40.00
Why it works
Each window holds the k most recent values, so its average follows the trend while the ups and downs inside the window cancel out.
Examples
Example 1 · Input
3 10 20 30 40 50
Expected output
20.00 30.00 40.00
Example 2 · Input
2 1 2 4 8
Expected output
1.50 3.00 6.00
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The window ending at position i is values[i - k + 1 : i + 1].
Hint 2
The first full window ends at position k − 1.
Solution
Show one correct solution (try it yourself first)
k = int(input())
values = list(map(float, input().split()))
out = []
for i in range(k - 1, len(values)):
window = values[i - k + 1:i + 1]
out.append(f"{sum(window) / k:.2f}")
print(" ".join(out))