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!
[1,3,1]
return? \$\endgroup\$ – Leaky Nun May 10 '16 at 7:409
? (Like3.2.9
will work but not3.2.10
) \$\endgroup\$ – Leaky Nun May 10 '16 at 8:52