38
5
Don't you love those exploded-view diagrams in which a machine or object is taken apart into its smallest pieces?
Let's do that to a string!
The challenge
Write a program or function that
- inputs a string containing only printable ASCII characters;
- dissects the string into groups of non-space equal characters (the "pieces" of the string);
- outputs those groups in any convenient format, with some separator between groups.
For example, given the string
Ah, abracadabra!
the output would be the following groups:
! , A aaaaa bb c d h rr
Each group in the output contains equal characters, with spaces removed. A newline has been used as separator between groups. More about allowed formats below.
Rules
The input should be a string or an array of chars. It will only contain printable ASCII chars (the inclusive range from space to tilde). If your language does not support that, you can take the input in the form of numbers representing ASCII codes.
You can assume that the input contains at least one non-space character.
The output should consist of characters (even if the input is by means of ASCII codes). There has to be an unambiguous separator between groups, different than any non-space character that may appear in the input.
If the output is via function return, it may also be an array or strings, or an array of arrays of chars, or similar structure. In that case the structure provides the necessary separation.
A separator between characters of each group is optional. If there is one, the same rule applies: it can't be a non-space character that may appear in the input. Also, it can't be the same separator as used between groups.
Other than that, the format is flexible. Here are some examples:
The groups may be strings separated by newlines, as shown above.
The groups may be separated by any non-ASCII character, such as
¬
. The output for the above input would be the string:!¬,¬A¬aaaaa¬bb¬c¬d¬h¬rr
The groups may be separated by n>1 spaces (even if n is variable), with chars between each group separated by a single space:
! , A a a a a a b b c d h r r
The output may also be an array or list of strings returned by a function:
['!', 'A', 'aaaaa', 'bb', 'c', 'd', 'h', 'rr']
Or an array of char arrays:
[['!'], ['A'], ['a', 'a', 'a', 'a', 'a'], ['b', 'b'], ['c'], ['d'], ['h'], ['r', 'r']]
Examples of formats that are not allowed, according to the rules:
- A comma can't be used as separator (
!,,,A,a,a,a,a,a,b,b,c,d,h,r,r
), because the input may contain commas. - It's not accepted to drop the separator between groups (
!,Aaaaaabbcdhrr
) or to use the same separator between groups and within groups (! , A a a a a a b b c d h r r
).
The groups may appear in any order in the output. For example: alphabetical order (as in the examples above), order of first appearance in the string, ... The order need not be consistent or even deterministic.
Note that the input cannot contain newline characters, and A
and a
are different characters (grouping is case-sentitive).
Shortest code in bytes wins.
Test cases
In each test case, first line is input, and the remaining lines are the output, with each group in a different line.
Test case 1:
Ah, abracadabra! ! , A aaaaa bb c d h rr
Test case 2:
\o/\o/\o/ /// \\\ ooo
Test case 3:
A man, a plan, a canal: Panama! ! ,, : A P aaaaaaaaa c ll mm nnnn p
Test case 4:
"Show me how you do that trick, the one that makes me scream" she said "" , S aaaaa cc dd eeeeeee hhhhhh ii kk mmmm n ooooo rr ssss tttttt u ww y
1If we use non-ASCII symbols like "¬" as separator, can it be counted as 1 byte? – Leaky Nun – 2016-08-24T22:39:35.527
5@LeakyNun No, it will be counted as it corresponds depending on the encoding used for the source code, as usual – Luis Mendo – 2016-08-24T22:40:31.503
Is a trailing newline after the last group acceptable? – None – 2016-08-24T23:10:42.463
Is a leading newline of output acceptable? – James – 2016-08-24T23:16:07.843
@DJMcMayhem Sure. That can't be mistaken with any input char – Luis Mendo – 2016-08-24T23:17:08.213
Argh! Im too late to this party. A patch to SILOS was written at 12:00 AM this morning, It now can actually accept multiline string input. but it now would be noncepeting with this feature – Rohan Jhunjhunwala – 2016-08-25T14:16:35.157
@RohanJhunjhunwala Can't you take input as an array of ASCII codes? I included that specially for you! – Luis Mendo – 2016-08-25T14:52:34.257
@LuisMendo ok I will. – Rohan Jhunjhunwala – 2016-08-25T15:06:45.513
Your third and fourth output format examples step from 5 'a's to 4 'a's. – Delioth – 2016-08-25T15:40:05.430
@Delioth Thanks! Corrected – Luis Mendo – 2016-08-25T15:57:13.057
I think the proper term for this is "knolling": https://en.wikipedia.org/wiki/Knoll_(verb)
– Michael Schumacher – 2016-08-25T19:28:23.013@LuisMendo I'm almost there, One clarification can I output unpaced groups that have no members? Essentially my output is separated with n >= 1 new lines where n is variable – Rohan Jhunjhunwala – 2016-08-25T20:03:39.793
@LuisMendo I did it! – Rohan Jhunjhunwala – 2016-08-25T20:24:23.437
1@RohanJhunjhunwala Well done! :-) Yes, several newlines as separators is fine – Luis Mendo – 2016-08-25T21:35:55.683