If you sort a string you'll typically get something like:
':Iaaceeefggghiiiiklllllmnnooooprrssstttttuuyyyy
Yes, that was the first sentence sorted.
As you can see, there are a lot of repeated characters, aa
, eee
, ttttt
, 9 spaces and so on.
If we add 128
to the ASCII-value of the first duplicate, 256
to the second, 384
to the third and so on, sort it again and output the new string (modulus 128 to get the same characters back) we get the string:
':Iacefghiklmnoprstuy aegilnorstuy egilosty iloty lt
(Note the single leading space and the 4 trailing spaces).
The string is "sequentially sorted" <space>':I....uy
, <space>aeg....uy
, <space>egi....ty
, <space>iloty
, <space>lt
, <space>
, <space>
,<space>
, <space>
.
It might be easier to visualize this if we use a string with digits in it. The string 111222334
will when "sorted" be: 123412312
.
Challenge:
To no surprise, the challenge is to write a code that sorts a string according to the description above.
You can assume that the input string will contain only printable ASCII-characters in the range 32-126 (space to tilde).
Test cases:
**Test cases:**
*:Tacest*es*s*
If you sort a string you'll typically get something like:
':Iacefghiklmnoprstuy aegilnorstuy egilosty iloty lt
Hello, World!
!,HWdelorlol
#MATLAB, 114 bytes
#,14ABLMTbesty 1A
f=@(s)[mod(sort(cell2mat(cellfun(@(c)c+128*(0:nnz(c)-1),mat2cell(sort(s),1,histc(s,unique(s))),'un',0))),128),''];
'()*+,-0128:;=@[]acdefhilmnoqrstuz'(),0128@acefilmnorstu'(),12celmnostu'(),12celnstu(),clnst(),cls(),cs(),()()()()
This is code-golf, so the shortest code in each language counted in bytes will winref.
,Safginorst orst ort
:P \$\endgroup\$