g | x | w | all
Bytes Lang Time Link
025sclin240903T150403ZMama Fun
073Clojure240903T141743ZNikoNyrh
055Python 3240708T082949Zno comme
041Ruby240708T110352ZG B
00605AB1E240708T065138ZKevin Cr
042Desmos240707T012010ZAiden Ch
043Charcoal240707T205545ZNeil
039R240706T163029Zpajonk
023Wolfram Language Mathematica240706T223148ZGreg Mar
060Google Sheets240706T204015Zz..
031Octave with Image Package240706T155203ZLuis Men
047Arturo240706T193809Zchunes
058Python 3240706T181911ZMukundan
003Jelly240706T142512ZJonathan
008Japt240706T121748ZShaggy
030APL+WIN240706T125528ZGraham
013Uiua SBCS240706T113354Zchunes
003Jelly240706T111628Zlyxal
024Factor240706T103142Zchunes
067JavaScript Node.js240706T095318Zl4m2
004Vyxal240706T100546Zemanresu

sclin, 25 bytes

%`"."map
""sort"len"Q2/ :

Try it on scline!

Explanation

Prettified code:

%` (.) map
() sort \len Q 2/ :

Clojure, 73 bytes

#(apply map(fn[& l](nth(sort l)(quot %2 2)))(for[i(range %2)](drop i %)))

The anonymous function takes K (same as the anonymous function's 2nd parameter %2) arguments, returning the median. TIO.

Python 3, 55 bytes

f=lambda x,k:x[k-1:]and[sorted(x[:k])[k//2]]+f(x[1:],k)

Try it online!

Ruby, 41 bytes

->l,n{l.each_cons(n).map{|a|a.sort[n/2]}}

Try it online!

05AB1E, 6 bytes

ŒIù€Åm

Try it online.

Explanation:

Œ       # Get all sublists of the first (implicit) input-list
 Iù     # Only keep the sublists of a length of the second input-integer
   €    # Map over each k-sized sublist:
    Åm  #  Calculate the median
        # (after which the resulting list is output implicitly)

Desmos, 44 42 bytes

f(L,N,K)=[L[i-K+1...i].medianfori=[K...N]]

Try It On Desmos!

Try It On Desmos! - Prettified

Charcoal, 43 bytes

NθFN⊞υNFE…·θLυ✂υκι¹«W›⊗L⁻ι⟦⌈ι⟧θ≔⁻ι⟦⌈ι⟧ι⟦I⌈ι

Try it online! Link is to verbose version of code. Takes K as the first input and N as the second input. Explanation: This was tricky without resorting to eval hacks to invoke external sort or median functions.

NθFN⊞υN

Input K, N and the input array.

FE…·θLυ✂υκι¹«

Loop over all of the slices of the input array of length K.

W›⊗L⁻ι⟦⌈ι⟧θ

While removing the highest remaining element from the slice would still leave the median in the remaining elements...

≔⁻ι⟦⌈ι⟧ι

... remove the highest remaining element.

⟦I⌈ι

Output the median, which is now the highest remaining element.

The above implementation is probably O(NK²). 65 bytes for a more efficient O(NK) version:

NθFN⊞υN≔⟦⟧ζ≔⟦⟧εFυ«FΦζ›κι⊞ε⊟ζ⊞ζιWε⊞ζ⊟ε¿⁼Lζθ«≔⌕ζ§υⅉδ⟦I§ζ÷θ²⟧≔Φζ⁻λδζ

Try it online! Link is to verbose version of code. Takes K as the first input and N as the second input. Explanation:

NθFN⊞υN

Input K, N and the input array.

≔⟦⟧ζ≔⟦⟧ε

Start with an empty sorted window and an empty temporary array.

Fυ«

Loop over the input array.

FΦζ›κι⊞ε⊟ζ⊞ζιWε⊞ζ⊟ε

Insert the current element into the sorted position in the current window in O(K) time.

¿⁼Lζθ«

If the sorted window has length K, then...

≔⌕ζ§υⅉδ

Find the index of the element of the window to remove before the next loop.

⟦I§ζ÷θ²⟧

Output the middle element of the array.

≔Φζ⁻λδζ

Remove the element that's about to drop out of the window.

R, 45 39 bytes

Edit: -6 bytes thanks to @Dominic van Essen.

\(x,N,K)Map(\(i)median(x[1:K+i]),K:N-K)

Attempt This Online!


R + zoo, 15 bytes

zoo::rollmedian

Attempt This Online!

(Doesn't use the input N)

Wolfram Language (Mathematica), 23 bytes

Median/@##~Partition~1&

Try it online! Unnamed function expecting two arguments in the format [{3,5,8,2,4,7,5,2,7,4},3]. ##~Partition~1 is the same as Partition[#1,#2,1], which creates the array of length-#2 sublists of #1.

Google Sheets, 60 bytes

=MAP(SEQUENCE(B1-B2+1),LAMBDA(i,MEDIAN(OFFSET(A4,i-1,,B2))))

enter image description here

Octave with Image Package, 31 bytes

@(x,k)median(im2col(x,[1 k]),1)

Try it online!

How it works

@(x,k) defines an anonymous function inputs x (row vector) and k (scalar).

im2col(x,[1 k]) produces sliding blocks from x of length k, arranged as columns of a matrix. For inputs x = [3 5 8 2 4 7 5 2 7 4], k = 3 this gives

3   5   8   2   4   7   5   2
5   8   2   4   7   5   2   7
8   2   4   7   5   2   7   4

median(...,1) computes the median along the first dimension, that is, vertically.

Arturo, 47 bytes

$[a,k]->map 0..-size a k'i->median a\[i..i+k-1]

enter image description here

Needs modern Arturo so median isn't bugged. So no online link.

Python 3, 58 bytes

lambda n,x,k:[sorted(x[i:i+k])[k//2]for i in range(n-k+1)]

Try it online!


Python 3, 61 bytes

Does not require n

lambda x,k:[sorted(x[i:i+k])[k//2]for i in range(len(x)-k+1)]

Try it online!

Jelly,  4  3 bytes

ṡÆṁ

A dyadic Link that accepts the list of orderable numbers on the left and the infix length on the right and yields a list of numbers.

Try it online!

How?

ṡÆṁ - Link: list of numbers L, positive integer N
ṡ   - overlapping slices of {L} of length {N}
 Æṁ - median (vectorises)

Japt, 8 bytes

ãV ËÍgVz

Try it

ãV ËÍgVz     :Implicit input of array U & integer V
ãV           :Sub-arrays of U of length V
   Ë         :Map
    Í        :  Sort
     g       :  Get element at 0-based index
      Vz     :    V floor divided by 2

APL+WIN, 30 bytes

Prompts for vector of numbers followed by k

(⌈.5×k)⌷¨(⊂¨⍒¨i)⌷¨i←(k←⎕),/v←⎕

Try it online! Thanks to Dyalog Classic

Uiua SBCS, 13 bytes

⊡⌊÷2⊸⧻⍉≡⊏⊸≡⍏◫

Try it!

⊡⌊÷2⊸⧻⍉≡⊏⊸≡⍏◫­⁡​‎‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌­
            ◫  # ‎⁡windows of length k
       ≡⊏⊸≡⍏   # ‎⁢sort each window
      ⍉        # ‎⁣transpose
⊡⌊÷2⊸⧻         # ‎⁤get the middle row

Jelly, 3 bytes

ṡÆṁ

Try it online!

Is vectorising median a good golflang design choice? I don't know. But it definitely helps here.

Takes the list then k.

Explained

ṡÆṁ­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌­
ṡ    # ‎⁡Get all overlaps in the list of length k
 Æṁ  # ‎⁢Find the median of each
💎

Created with the help of Luminespire.

Factor, 24 bytes

[ clump [ median ] map ]

Try it online!

JavaScript (Node.js), 67 bytes

x=>k=>g=n=>n<k?[]:[...g(n-1),x.slice(n-k,n).sort((p,q)=>p-q)[k>>1]]

Try it online!

JavaScript (Node.js), 76 71 bytes

Don't read n

x=>n=>x.flatMap(c=>++k<0?[]:x.slice(k,k+n).sort((p,q)=>p-q)[n>>1],k=-n)

Try it online!

Both functions can reduce 10 bytes if input as Uint32Array

Vyxal, 4 bytes

lv∆ṁ

Try it Online!

l    # Get all overlapping slices of length k
 v∆ṁ # Take the median of each