# Convert header levels to numbers

### 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.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]]


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!

• What would [1,3,1] return? – Leaky Nun May 10 '16 at 7:40
• Why should people bother to write a reverse program if it will not contribute to the score? – Bassdrop Cumberwubwubwub May 10 '16 at 8:48
• Can I only make it work to, you know, 9? (Like 3.2.9 will work but not 3.2.10) – Leaky Nun May 10 '16 at 8:52
• Maybe you could add some test cases? – R. Kap May 10 '16 at 9:06
• You 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 May 11 '16 at 8:04

## CJam (15 + 4 = 19 bytes)

Mq~{1\$0+<))+}%


Online demo

To reverse,

q~:,


## JavaScript (ES6), 47 + 21 = 68 bytes

a=>a.map(l=>b=[...b.slice(0,--l),-~b[l]],b=[])


Explanation:

a=>a.map(l=>            Loop over each header level
b=[                    Make a new sublist using
...b.slice(0,--l),    First l-1 elements from the previous sublist
-~b[l]],              Plus increment the lth element or default to 1


The bitwise not operator is used because it will coerce its operand to a signed 32-bit integer, which will be zero if this is a new level. Negating the result conveniently returns one more than the original number, or 1 in the case of a new level. Reverse process:

a=>a.map(b=>b.length)

• Could you maybe explain? – Adám May 14 '16 at 22:58
• @Nᴮᶻ Writing the explanation exposed a bug in the code. The new code is sadly a byte longer. – Neil May 14 '16 at 23:40

# Jelly, 10 8 bytes

### Forward, 8 6 bytes

ḣ+Ṭ}ð\


Try it online!

### Backward, 2 bytes

L€


Try it online!

## How it works

### Forward

ḣ+Ṭ}ð\  Main link. Argument: A (list of integers)

ð\  Cumulatively reduce A by the dyadic chain to the left. Arguments: x, y
ḣ       Head; Truncate x to y items.
Ṭ}    Untruth; yield a list of y-1 zeroes and 1 one.
+      Compute the vectorized sum of both results.


### Backward

L€      Main link. Argument: A (nested list)

L       Length.
€      Map the previous link over all items in A.


# Pyth, 27 22 + 2 = 24 bytes

This is embarrassingly more than 1 less than half of the bytes that javascript used.

JylQjRJ.uhs*N^JY-VtQQ1


Try it online!

### Reverse, 2 bytes

lM


Try it online!

• Even more embarrassingly, it's longer than the CJam solution. – Peter Taylor May 11 '16 at 8:48
• At least the reverse solution is better. (And with the scores combined you're now only just over a third of the bytes of ES6.) – Neil May 11 '16 at 11:35

# Pyth, 11 + 2 = 13 bytes

Forward:

m=X_1<+Y0d1


Demonstration

Backward:

lM


I like this one.

How it works:

m=X_1<+Y0d1
Implicit: Y = []
m              Map over the input, with d being the map variable
+Y0      Add a 0 on to the end of Y
<   d     Slice down to the first d elements
X_1     1    Increment the last one
=             Assign the result back to Y


The resultant list will contain each value that Y takes on, which is the desired sequence.

Embarrassment cured.

# Ruby, 44 + 17 = 61 bytes

Forward:

->x{l=[]
x.map{|e|l=l[0,e-=1]<<(l[e]||0)+1}}


Backward:

->x{x.map &:size}
`