4
1
Introduction
Nodes in tree structures (e.g. headings in a long document, law paragraphs, etc.) are often created by assigning a specific header level (h1, h2, etc.) to each item, but outputted with additional node numbers for each level (1., 1.1., etc.), e.g.:
1. Intro (h1)
1.1. About this (h2)
1.2. Target audience (h2)
2. Actual stuff (h1)
3. More stuff (h1)
3.1. Details (h2)
3.1.1. Special points (h3)
3.2. Specifics (h2)
3.2.1. Minutiae (h3)
3.2.2. Petitesses (h3)
3.3. Specifications (h2)
4. Index (h1)
5. Appendices (h1)
Input
Given (by any means) a list of header levels (the X's in hX above) like:
[1,2,2,1,1,2,3,2,3,3,2,1,1]
Input will always begin with a 1, and Inputn+1 is in the range [1,1+Inputn].
Input will only contain numbers in the range [1,256].
Output
Return (by any means) the list of lists (or matrix - see below) of node numbers (dot-separated above):
[[1],[1,1],[1,2],[2],[3],[3,1],[3,1,1],[3,2],[3,2,1],[3,2,2],[3,3],[4],[5]]
You may return scalars instead of one-element sub-lists:
[1,[1,1],[1,2],2,3,[3,1],[3,1,1],[3,2],[3,2,1],[3,2,2],[3,3],4,5]
You may return a matrix (or list of lists) which is padded with zeros so that all rows (sub-lists) have the same length:
[[1,0,0]
[1,1,0]
[1,2,0]
[2,0,0]
[3,0,0]
[3,1,0]
[3,1,1]
[3,2,0]
[3,2,1]
[3,2,2]
[3,3,0]
[4,0,0]
[5,0,0]]
Additional rules
The reverse process of all this is just the length of each sub-list. You must also include a reverse program in the same language. Your score is the total byte count of your two programs. If you return zero-padded results on the first prgram, your reverse code must handle such input.
Please explain your code so we all can learn from it.
This is code golf, because I'll need to read it on a phone screen and type it into a computer. - No, really, this is not meme-ness!
3What would
[1,3,1]
return? – Leaky Nun – 2016-05-10T07:40:04.8336Why should people bother to write a reverse program if it will not contribute to the score? – Bassdrop Cumberwubwubwub – 2016-05-10T08:48:18.797
Can I only make it work to, you know,
9
? (Like3.2.9
will work but not3.2.10
) – Leaky Nun – 2016-05-10T08:52:05.5473Maybe you could add some test cases? – R. Kap – 2016-05-10T09:06:53.370
@KennyLau I updated the rules to state that
[1,3,1]
is invalid input and that any positive input number below 257 is valid. – Adám – 2016-05-11T06:08:28.880@BassdropCumberwubwubwub Now it IS counted. – Adám – 2016-05-11T06:10:37.130
@R.Kap What is lacking in the existing test case? – Adám – 2016-05-11T06:11:45.417
2You should be more up front that the total score is the sum of the original and reversed program. It's easy to miss tucked away in an additional rule. – xnor – 2016-05-11T08:04:26.333
@xnor forgot to say I did that – Adám – 2016-05-14T22:57:44.423
Returning scalars and one-element lists is both allowed, but what about mixing the two? Is
[1, [1, 1], [1, 2], [2], [3], ...]
an acceptable output for first program if the reverse program can handle it? – Dennis – 2016-05-15T01:54:05.263@Dennis Allowed if reverse can handle it. In any case, it would be trivial to force all elements into lists/scalarize singletons. – Adám – 2016-05-16T06:41:50.747