29
1
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.
Title is a bit confusing, resulting in me thinking this and ignoring the description: https://tio.run/nexus/05ab1e#@1@td2jh4ZVe//@rW3kmJqempWdkZufk5uUXFBWXlFYqJKamZ@bk5UM4IHZ@cUmlApACkjklCgoKAA Nice challenge otherwise, I'll work on expanding that to meet the brief.
– Magic Octopus Urn – 2017-01-10T21:40:48.867Can we output a list of characters instead of a string? – Post Rock Garf Hunter – 2017-01-10T21:52:51.653
If you can input a string, then the output should be a string too. If a list of characters is the normal way of inputting and outputting strings in your languages then it's OK. You can for instance not output
{'S', 'g', 'i', 'n', 'r', 't'}
in Python, since the "normal" way to do it is"String"
. – Stewie Griffin – 2017-01-10T21:54:52.770I'll correct my comment above: a string is a list of characters, so a list of characters is accepted output. However, a list of strings is not accepted. This means, if it's possible to add a second character to an element in your list then it's not accepted. As an example:
{'a','b'}
is not accepted in Matlab since you can add a character to each of the characters like this:{'aa','b'}
. Your input and output must be on the same format. – Stewie Griffin – 2017-01-10T23:40:53.673@StewieGriffin When you say sorted according to the description above. Do you mean my sort algorithm must follow the process of modifying ASCII values or it just has to produce the same output as that algorithm? – George Reith – 2017-01-11T00:01:21.137
No, you only need the same result. So far, I think only one answer has used the same algorithm. – Stewie Griffin – 2017-01-11T00:14:54.680
Proposed alternate title:
,Safginorst orst ort
:P – James – 2017-01-12T18:03:17.087@DJMcMayhem haha, nice and descriptive title... It's actually possible to pronounce, so it could work :) – Stewie Griffin – 2017-01-12T22:21:44.940
It would be funny if someone made an answer that was already sorted |:) – ckjbgames – 2017-02-03T18:34:10.823