○ Signal processing · sliding windows
Moving-average filter
Smooth a noisy signal by averaging a window that slides along it, then plot both.
- Level 3 · Innovator
- 5 tests
- 🐞 debug variant
- technically checked
Your task
A moving average replaces each point with the mean of a window of w neighbouring samples. It is the simplest low-pass filter: it keeps slow trends and suppresses fast noise.
Write movingAverage(xs, w) that returns the averages of every complete window, in order: the first output averages xs[0..w−1], the next xs[1..w], and so on. The output has xs.length − w + 1 values; if w is larger than the signal, return an empty array.
Hints (0 of 3 shown)
What gets checked
- movingAverage([1, 2, 3, 4, 5], 3) → [2, 3, 4]
- movingAverage([10, 20], 1) → [10, 20]
Plus 3 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.
Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments
Press Run to see what your program prints.
Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.