36
2
Given an ordered list of same-case letter strings (a-z XOR A-Z) where each string is preceded by 0 or more space ( ) characters, output the same list but with the strings sorted at each level of indentation. Indentation depths under different parents count as distinct lists for sorting purposes.
Example
If your input is:
bdellium
fox
hound
alien
aisle
wasabi
elf
alien
horseradish
xeno
irk
wren
tsunami
djinn
zebra
your output should be
aisle
horseradish
xeno
wasabi
alien
elf
bdellium
alien
fox
hound
djinn
zebra
irk
tsunami
wren
If you like, think of it like a directory listing, and you need to sort the names within each directory.
Minutiae
- An item may be indented by any number of spaces. If it is indented by the same number of spaces as the previous item it belongs in the same sort hierarchy as the previous item. If it is indented by more spaces it is the start of a new sub-hierarchy.
- If a line is indented by fewer spaces than the line above it, it links up to the closest sub group above it with the same # or fewer spaces before it (like horseradish in the above example, which links onto the wasabi group above it because wasabi is the first item above it to not have more spaces than horseradish)
- You must preserve the indenting level of each input item in your output
- Tabs in the output are disallowed
- The first line of the input will never be indented
- Your program must handle at least one of all-uppercase and all-lowercase strings; it doesn't have to handle both.
Scoring
This is a code-golf, so the answer which uses the fewest bytes wins.
7Nice challenge! – Adám – 2018-08-21T22:22:52.647
1
Btw, for next time, consider using the Sandbox to iron out issues with a challenge before posting it to the main site.
– Adám – 2018-08-21T22:34:40.820May we take input as a list of strings and a list of indentation levels? E.g.
[0,2,2,2,0,2,4,4,2,4,0,0,0,0,6]
and["bdellium","fox","hound","alien","aisle","wasabi","elf","alien","horseradish","xeno","irk","wren","tsunami","djinn","zebra"]
? – Adám – 2018-08-21T22:41:26.5308@Adám No, the recursive string parsing logic required is the reason I wrote this prompt. – Techrocket9 – 2018-08-21T22:43:35.940
1By 'indenting level', do you mean the amount of spaces or where it is in relation to the previous level? – Jo King – 2018-08-21T23:35:24.753
2If the input is
['a','..b', '.c', '..d']
, what should the output be?['a','..b', '.c', '..d']
or['a','.c','..b', '..d']
or some other thing? (I'm using'.'
instead of space for visual clarity). – Chas Brown – 2018-08-22T00:03:57.7871I approve of the BNL reference :) – Not that Charles – 2018-08-22T02:06:46.990
1@JoKing The number of spaces before the letters. Each string of the output should have the same number of spaces before it as it does in the input. – Techrocket9 – 2018-08-22T02:21:06.513
1@ChasBrown The first one, ['a','..b', '.c', '..d']. Whether or not a new subgroup forms depends only on whether or not the current line has more spaces before it than the previous line. In your example b has more spaces than a so it's grouped under a. c doesn't have more spaces than b so it ends up in the same sorting group as b. d has more spaces than c so it gets its own subgroup under c. I suppose the implicit consequence of this is that if you have fewer spaces than the line above you that line's subgroup ends and you go up until you find a subgroup you match or exceed the spacing of. – Techrocket9 – 2018-08-22T02:42:20.877
1This is an indentation preserving sort, not an indentation based sort. – Pureferret – 2018-08-22T14:13:51.713
1@Pureferret The grouping levels are based on the level of indentation. It's also indentation preserving, but that's a secondary characteristic. – Techrocket9 – 2018-08-22T14:21:59.697
will there be any spaces in the strings? e.g. " two words" – streetster – 2018-08-22T19:37:04.080
1do we have to handle where the first item in the sub-group is at a lower level in the heirarchy? or will the levels increase? – streetster – 2018-08-22T20:20:04.530
2@streetster strings (a-z XOR A-Z) – Adám – 2018-08-22T21:12:39.240
@streetster The way it's written right now that has to be handled. It should have no impact on the groupings though (in the above example, if fox had an additional two spaces before it fox would retain the same position in the output, just with more spaces). I don't particularly like this edge-case (since the output may not be idempotent if re-run through the program), but now that there are a number of established answers I'm reluctant to change the rules. – Techrocket9 – 2018-08-22T21:37:14.643
1I feel like the
first line of the input will never be indented
should apply recursively, so that the case that @ChasBrown describes should never happen – Jo King – 2018-08-22T22:57:43.557@Techrocket9, what if alien had an extra space? Seems a few of us are asking about this case. I agree with Jo King ^^ – streetster – 2018-08-23T06:32:09.570