New answers tagged

0

05AB1E, 14 bytes DāR<IÖ≠©Ï{®ƶ<ǝ I/O of the string as a list of characters. Try it online or verify all test cases (feel free to remove the S and J in the test suite to see the actual I/O with character lists). And I agree with @mbomb007's comment, although it's still pretty short this way. :) Explanation: # Example inputs: s=["a",...


0

Python 3.8 (pre-release), 111 109 bytes lambda n,s:(l:=len(s),r:=sorted(s[~i]for i in range(l)if i%n),[r.insert(i,s[i])for i in range(~-l%n,l,n)])[1] Try it online!


1

Perl 6, 53 bytes {(my@a=$^a.comb)[grep (@a-*-1)%$^n,^@a].=sort;[~] @a} Try it online! It feels like there should be a shorter way of doing this


1

Burlesque, 36 bytes pe<-jJ-.hdcoJ)-]j)[-++<>#acoz[\[\[<- Try it online! Takes input as N "String" pe # Parse and push <- # Reverse the string j # Put N at the top of the stack J-.hd # Store N-1 for later co # Split string into chunks of N J)-] # Duplicate and take the heads j)[- # Take the tails of the ...


0

Burlesque, 6 bytes rasgtp Try it online! ra # Read input as array sg # Sort and group like values tp # Transpose


0

05AB1E, 4 bytes {γζþ Try it online or verify all test cases. Explanation: { # Sort the (implicit) input-list # i.e. [5,9,12,5,2,71,23,4,7,2,6,8,2,4,8,9,0,65,4,5,3,2] # → [0,2,2,2,2,3,4,4,4,5,5,5,6,7,8,8,9,9,12,23,65,71] γ # Group it into chunks of the same subsequent value # → [[0],[2,2,2,2],[3],[4,4,4],[5,5,5],[6],[7],[8,8],...


3

Wolfram Language (Mathematica), 219 188 184 bytes F@A_:=MinimalBy[##&@@@f/@#[[1]]&@*(FindPermutation@A~PermutationProduct~#&)/@GroupElements@PermutationGroup[Cycles@*List/@Join@@((f=Partition[#,2,1]&)/@Values@PositionIndex@A)],Length] Try it online! -31 bytes thanks to @Chris -4 bytes by making the code much faster (no more ...


3

Jelly, 51 43 bytes ṪµṢ,$Ḣ©€E¬a="ʋṚƊ×\ẸÞṪTḢ,®ṛ¦Ẹ}¡@¥ WÇẸпFĖẠƇÄ Try it online! A pair of links which when called as a monad returns a list of pairs of 1-indexed indices to swap. Based on the clever J algorithm developed by @Jonah, so be sure to upvote that one too! Explanation Helper link Takes a list containing a list of the remaining items to be ...


5

J, 126 bytes [:>[:{:"1@}./:~(({.@I.@:~:>@{.)({:@](|.@:{`[`]}~;])[,[:(0{*/,&I.{.)(<0{i.@$)*"1{"0 1=|.@])(,:>@{.))^:(-.@-:>@{.)^:a:;&(0 2$'') Try it online! Here is the same code in a more procedural form, if you're looking for an explanation of algorithm. f=. 3 :0 sorting=.y NB. copy of input we'll mutate sorted=./:~y swaps=.0 2$'' ...


0

R, 41 bytes split(sort(s<-scan()),sequence(table(s))) Try it online! Like Robin Ryder's answer, this makes use of table to get counts of the unique elements in the input. A fuller explanation to follow.


0

Zsh, 58 bytes for x (${(u)@})<<<$x&&argv[$@[(i)$x]]= <<<" ${*:+`$0 $@`}" Try it online! Yay, recursion! For each element $x of (u)nique ${@}rguments, print it, and set the first (i)nstance of it to the empty string. Finally, print a newline as a list delimiter, and if the remaining arguments are nonempty, substitute $0 $@, which ...


0

C (gcc), 114 bytes W;*P;S(D,E)int*D,*E;{do for(P=D;P<E;*P==W?printf("%d ", W),*P=*D,*D++=W,P=E:++P);while(++W);puts("");D-E&&S(D,E);} This solution takes a while to run because it iterates over all possible integer values from -2^31 to 2^31-1. Here is a TIO link to a slightly longer solution that runs a lot faster, the only difference is, that ...


1

Perl 6, 25 bytes *.classify({%.{$_}++}){*} Try it online! Groups each element of the input by how many times it has appeared in the sequence so far, then take only the values


3

R, 54 bytes for(i in 1:max(t<-table(scan())))print(names(t)[t>=i]) Try it online! Builds a table of the counts of the values. For input 1 3 4 2 6 5 1 8 3 8, this gives t = 1 2 3 4 5 6 8 2 1 2 1 1 1 2 where the first row is names(t) (the different values in the input) and the second is the counts in t. Then in the for loop, at iteration i, ...


Top 50 recent answers are included