| Bytes | Lang | Time | Link |
|---|---|---|---|
| 015 | Pip xp | 211210T165504Z | DLosc |
| 146 | tinylisp | 220208T150111Z | Giuseppe |
| 053 | Wolfram Language Mathematica | 211210T195032Z | ZaMoC |
| 020 | BQN | 211213T223240Z | DLosc |
| 071 | Haskell | 211213T013749Z | Madison |
| 131 | PHP | 211214T090241Z | Kaddath |
| 010 | 05AB1E | 211210T175138Z | Kevin Cr |
| 007 | Vyxal | 211212T000610Z | lyxal |
| 142 | Bash 4.4.23 | 211211T214852Z | Ité |
| 059 | R | 211210T171615Z | Giuseppe |
| 053 | Pari/GP | 211211T015120Z | alephalp |
| 070 | C gcc | 211210T235339Z | Noodle9 |
| 050 | Python 2 | 211210T233649Z | dingledo |
| 006 | Husk | 211210T200050Z | Dominic |
| 006 | Stax | 211210T222223Z | recursiv |
| 017 | HBL | 211210T203849Z | DLosc |
| 040 | Charcoal | 211210T200216Z | Neil |
| 024 | Retina | 211210T201621Z | Neil |
| 059 | R | 211210T195019Z | Dominic |
| 2415 | J | 211210T190854Z | Jonah |
| 008 | Jelly | 211210T165229Z | caird co |
| 063 | Python | 211210T170221Z | pxeger |
| 049 | Ruby | 211210T170511Z | G B |
| 061 | JavaScript ES6 | 211210T165320Z | rydwolf |
| 010 | Jelly | 211210T165224Z | hyperneu |
Pip -xp, 24 23 18 15 bytes
SN*_^@^13MFa<>6
Explanation
SN*_^@^13MFa<>6
a First command-line arg
<>6 Group into length-6 chunks
MF Map this function to each chunk and then flatten the result list
by one level:
^13 Split 13 into a list of digits [1;3]
_^@ Split the argument at those indices
SN* Sort each sublist using numeric comparison
tinylisp, 194 170 146 bytes
(load library
(d C(q((L P)(i L(c(map head(zip L(1to P)))(i(e P 3)(C(t(t(t L)))1)(i(e P 2)(C(t(t L))3)(C(t L)2))))(
(d S(q((L)(map merge-sort(C L 1
-24 thanks to DLosc suggesting implementing take instead of the hodge-podge mess of cs I was using before. -24 again by implementing take more golfily.
Wolfram Language (Mathematica), 53 bytes
Sort/@TakeList[#,i=0;UpTo@Mod[++i,4]&/@#]/.{}->Set@$&
-6 bytes thanks to @alephalpha
-3 bytes thanks to @theorist
-2 bytes thanks to @att
BQN, 24 23 22 21 20 bytes
-1 thanks to Dominic van Essen
∧¨⊢⊔˜¯1+`(0=∾↑↕3)⥊˜≠
Anonymous tacit function that takes an array of numbers as its right argument. Run it online!
Explanation
The explanation uses an example argument of ⟨ 8 3 ¯1 6 ¯8 7 ¯1 ⟩:
∧¨⊢⊔˜¯1+`(0=∾↑↕3)⥊˜≠
( ) Generate the array ⟨ 1 1 0 1 0 0 ⟩:
↕3 Range(3): ⟨ 0 1 2 ⟩
↑ Prefixes: ⟨ ⟨⟩ ⟨ 0 ⟩ ⟨ 0 1 ⟩ ⟨ 0 1 2 ⟩ ⟩
∾ Join: ⟨ 0 0 1 0 1 2 ⟩
0= Equal 0: ⟨ 1 1 0 1 0 0 ⟩
⥊˜ Reshape it to
≠ the length of the argument: ⟨ 1 1 0 1 0 0 1 ⟩
+` Cumulative sum
¯1 starting at -1: ⟨ 0 1 1 2 2 2 3 ⟩
⊔˜ Using that list as a categorizer, group
⊢ the argument: ⟨ ⟨ 8 ⟩ ⟨ 3 ¯1 ⟩ ⟨ 6 ¯8 7 ⟩ ⟨ ¯1 ⟩ ⟩
∧¨ Sort each: ⟨ ⟨ 8 ⟩ ⟨ ¯1 3 ⟩ ⟨ ¯8 6 7 ⟩ ⟨ ¯1 ⟩ ⟩
Haskell, 76 71 bytes
import Data.List
f=(%)1
n%[]=[]
n%x=sort(take n x):(mod n 3+1)%drop n x
-5 bytes thanks to xnor
Calling f on a list chunk sorts it.
PHP, 131 bytes
function(&$a){for($a=array_chunk($a,3),$i=-1;$a[++$i];sort($a[$i]))!$a[$i][1]|$i%3?:array_splice($a,$i,0,[[array_shift($a[$i])]]);}
This is a first take, it feels awfully long, there has to be a better way.. damn these "array_" prefixes!
05AB1E, 10 bytes
3LI∍£€{ʒgĀ
Try it online or verify all test cases.
Or alternatively (thanks to @ovs for N6%tò):
.yN6%tò}€{
Try it online or verify all test cases.
Explanation:
3L # Push list [1,2,3]
I∍ # Extend the [1,2,3] list to the same size as the input-list
£ # Split the (implicit) input-list into those parts,
# which includes trailing empty lists at the end
€{ # Sort each inner list
ʒgĀ # Remove the empty lists:
ʒ # Filter the list of lists:
gĀ # Check if the list-length is not 0
# (after which the result is output implicitly)
.y # Adjacent group the (implicit) input-list by:
N # Push the 0-based group-by index
6% # Modulo-6
t # Take the square-root of that
ò # Round it to the nearest integer
}€{ # After the group-by: sort each inner list
# (after which the result is output implicitly)
The N6%tò results in the sequence [0,1,1,2,2,2,0,1,1,2,2,2,...], causing the adjacent group-by to split the input-list into parts of sizes [1,2,3,1,2,3,...], which is what we want.
The first program could be 9 bytes if the input-length is guaranteed to be \$n>1\$:
3LI∍£€{Ô¨
See all test cases, and how it unfortunately fails for [1]. (Adding Ć after I would fix that edge-case, but unfortunately we'll then have another 10-bytes alternative.)
Vyxal, 7 bytes
ẏǒʀÞṁvs
I'm way late to the party, but who cares when the program looks like it spells something with heavy skamtebord vibes.
Explained
ẏǒʀÞṁvs
ẏ # The range [0...len(input)]
ǒ # modulo each by 3 to get the chunk sizes
ʀ # cast each to range [0...n] for shape
Þṁ # mold the input to that, without repeating elements
vs # sort each sublist
Bash 4.4.23, 142 bytes
f()(
i=1
a=("$@")
while [ ${#a[@]} -gt 0 ]
do
r=$r"["$(printf "%s\n" "${a[@]:0:$i}" |sort -n)"]"
a=("${a[@]:${i}}")
((i=i==3?1:i+1))
done
echo $r
)
Explanation
i=1 > used to keep track of chunk size
a=("$@") > transforms the parameters as an array
while [ ${#a[@]} -gt 0 ] > while there is still some elements to "chuck sort"
do
r=$r[$(printf "%s\n" "${a[@]:0:$i}"|sort -n)] > construct the final result by picking only the required chunk from the array and sorts it. Store sort result in r and ad some delimiters
a=("${a[@]:${i}}") > remove processed chunk from the array
((i=i==3?1:i+1)) > set size of next chunk
done
echo $r
C (gcc), 73 70 bytes
l;f(a,n)int*a;{for(l=0;n;n-=l,a+=l,l%=3)qsort(a,++l,4,L"\x62b078bǃ");}
Inputs a pointer to an array of integers and its length (because pointers in C carry no length info).
Chunk sorts the array in place.
Husk, 6 bytes
mOC¢ḣ3
How?
ḣ3 # 1..3
¢ # repeated infinitely;
C # now split the input into sublists
# of these lengths (discarding any
# extra, unused lengths);
m # finally, for each sublist:
O # sort it.
HBL, 17 bytes
?.(1(%(0,.))('?(2,.)(+(?(<,<),
).1
Explanation
The first line is a recursive function that does most of the work. It takes two arguments, a list and an integer, where the integer represents the current chunk size (1, 2, or 3).
?.(1(%(0,.))('?(2,.)(+(?(<,<),
? If
. Argument 1
is truthy (non-empty):
(1 Construct a list from
, Argument 2
(0 ) Take that many elements of
. Arg1
(% ) Sorted
prepended to
('? Recursive call with two arguments:
(2,.) Drop (arg2) elements of arg1
and
(+ Increment
(? If
, Arg2
(< ) is less than
< 3:
, Arg2
Else, 0
Else, empty list
Then the main function on the second line is simply:
) Call the previous function with these arguments:
. Argument 1 of the main function
and
1 1
Charcoal, 48 40 bytes
≔⮌AθWθ⊞υE⊕﹪Lυ³⊟θIEυ✂⟦⌊ι⁻Σι⁺⌊ι⌈ι⌈ι⟧⁰χ⁻⁴Lι
Try it online! Link is to verbose version of code. Explanation: Charcoal really struggles here as it has no variable length split, flatten or sort primitive (at least not on TIO).
≔⮌Aθ
Reverse the input array.
Wθ
Repeat until the input array has been chunked.
⊞υE⊕﹪Lυ³⊟θ
Create a chunk of size depending on the number of chunks so far.
IEυ✂⟦⌊ι⁻Σι⁺⌊ι⌈ι⌈ι⟧⁰χ⁻⁴Lι
For each chunk, get the minimum, middle and maximum elements, but skip the middle if the chunk's size is less than 3 and skip the maximum if its size is less than 2.
Previous 48-byte version didn't require the input to be completely chunkable:
WθF³«⊞υ✂θ⁰⊕κ≔✂θ⊕κLθ¹θ»IEΦυι✂⟦⌊ι⁻Σι⁺⌊ι⌈ι⌈ι⟧⁰χ⁻⁴Lι
Try it online! Link is to verbose version of code. Explanation:
Wθ
Repeat until the input is empty.
F³«
Take three chunks.
⊞υ✂θ⁰⊕κ
Save the chunk.
≔✂θ⊕κLθ¹θ
Remove the chunk from the input.
»IEΦυι✂⟦⌊ι⁻Σι⁺⌊ι⌈ι⌈ι⟧⁰χ⁻⁴Lι
For each non-empty chunk, get the minimum, middle and maximum elements, but skip the middle if the chunk's size is less than 3 and skip the maximum if its size is less than 2.
Retina, 24 bytes
5,6,S`,
%,,2S`,
%N`[^,]+
Try it online! Explanation:
5,6,S`,
Split the input into groups of up to six values. Limits are 0-indexed in Retina 1, so the first six values are numbered 0 to 5 and therefore the comma after the sixth value is also numbered 5, however subsequent splits are every six commas as expected, until the end of the input.
%,,2S`,
Split each group into groups of 1, 2 and 3 values (if possible). The syntax ,,2 is short for ,2,2 so it only splits on the first and third comma; ,2 would be a range and so would also split on the second comma.
%N`[^,]+
Sort the values in each group.
Jelly, 10 8 bytes
3Rṁx@JṢƙ
How it works
3Rṁx@JṢƙ - Main link. Takes L on the left
3R - [1,2,3]
ṁ - Mold to length L, repeating
J - Yield [1,2,...,len(L)]
x@ - Repeat 1 once, 2 twice, 3 thrice, 4 once, 5 twice, etc.
Ṣƙ - Sort chunks of L, grouped by the elements of [1,2,2,3,3,3,4,...]
Python, 86 65 63 bytes
def f(a,i=0): for x in a:a[i:i%3-~i]=sorted(a[i:i%3-~i]),;i+=1
Modifies the list in-place; kinda cursed.
Ruby, 49 bytes
->l{x=5;l.chunk{441[(x+=1)%12]}.map{|a,b|b.sort}}
Literally: chunk, then sort.
The magic number 441 is a bitmask with 1,2,3 binary digits alternatively seto to 1 or 0:
000 11 0 111 00 1
Since there are only 2 binary digits, I can't make it 3 chunks, so I make it 6 and so I use mod 12 to rotate.
JavaScript (ES6), 61 bytes
-4 and -1 thanks to Arnauld
x=>(f=n=>x+x&&[x.splice(0,++n).sort((w,z)=>w-z),...f(n%3)])``
Jelly, 10 bytes
J%6B§=1ÄṢƙ
J%6B§=1ÄṢƙ Main Link
J [1, 2, 3, ..., len(input)]
%6 [1, 2, 3, 4, 5, 0, 1, 2, 3, ...]
B Binary; [[1], [1, 0], [1, 1], [1, 0, 0], [1, 0, 1], [0], ...]
§ Sum each; [1, 1, 2, 1, 2, 0, ...]
=1 Equal to 1?; [1, 1, 0, 1, 0, 0, ...]
Ä Cumulative sum; [1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 6, 6, ...]
Ṣƙ Sort chunks of the input grouped based on equal elements on the left side