Skip to content
Tactiverge

○ Signal processing · sorting small arrays

Median filter for glitches

Remove one-sample glitches from a distance sensor with a three-point median.

  • Level 3 · Innovator
  • 6 tests
  • technically checked

Your task

Ultrasonic distance sensors sometimes return a single absurd reading. A median of three replaces each value with the middle one of itself and its two neighbours, which deletes isolated glitches without blurring edges.

Write median3(xs) returning a new array of the same length where:

  • the first and last values are copied unchanged;
  • every other value i is the median of xs[i−1], xs[i], xs[i+1].

Hints (0 of 3 shown)

    What gets checked

    • median3([1, 1, 9, 1, 1]) → [1, 1, 1, 1, 1]
    • median3([3, 1, 2]) → [3, 2, 2]

    Plus 4 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

    Starting the sandbox…

    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.