| Bytes | Lang | Time | Link |
|---|---|---|---|
| 025 | sclin | 240903T150403Z | Mama Fun |
| 073 | Clojure | 240903T141743Z | NikoNyrh |
| 055 | Python 3 | 240708T082949Z | no comme |
| 041 | Ruby | 240708T110352Z | G B |
| 006 | 05AB1E | 240708T065138Z | Kevin Cr |
| 042 | Desmos | 240707T012010Z | Aiden Ch |
| 043 | Charcoal | 240707T205545Z | Neil |
| 039 | R | 240706T163029Z | pajonk |
| 023 | Wolfram Language Mathematica | 240706T223148Z | Greg Mar |
| 060 | Google Sheets | 240706T204015Z | z.. |
| 031 | Octave with Image Package | 240706T155203Z | Luis Men |
| 047 | Arturo | 240706T193809Z | chunes |
| 058 | Python 3 | 240706T181911Z | Mukundan |
| 003 | Jelly | 240706T142512Z | Jonathan |
| 008 | Japt | 240706T121748Z | Shaggy |
| 030 | APL+WIN | 240706T125528Z | Graham |
| 013 | Uiua SBCS | 240706T113354Z | chunes |
| 003 | Jelly | 240706T111628Z | lyxal |
| 024 | Factor | 240706T103142Z | chunes |
| 067 | JavaScript Node.js | 240706T095318Z | l4m2 |
| 004 | Vyxal | 240706T100546Z | emanresu |
sclin, 25 bytes
%`"."map
""sort"len"Q2/ :
Explanation
Prettified code:
%` (.) map
() sort \len Q 2/ :
%`chunk(.) mapmap over each chunk...() sortsort\len Q 2/ :get item at indexlength / 2(implicitly coerced to int)
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.
05AB1E, 6 bytes
ŒIù€Åm
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]]
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)
R + zoo, 15 bytes
zoo::rollmedian
(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.
Octave with Image Package, 31 bytes
@(x,k)median(im2col(x,[1 k]),1)
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]
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)]
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)]
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.
How?
ṡÆṁ - Link: list of numbers L, positive integer N
ṡ - overlapping slices of {L} of length {N}
Æṁ - median (vectorises)
Japt, 8 bytes
ãV ËÍgVz
ã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←⎕
Uiua SBCS, 13 bytes
⊡⌊÷2⊸⧻⍉≡⊏⊸≡⍏◫
⊡⌊÷2⊸⧻⍉≡⊏⊸≡⍏◫
◫ # windows of length k
≡⊏⊸≡⍏ # sort each window
⍉ # transpose
⊡⌊÷2⊸⧻ # get the middle row
Jelly, 3 bytes
ṡÆṁ
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.
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]]
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)

