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)


Given (by any means) a list of header levels (the X's in hX above) like:


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


Return (by any means) the list of lists (or matrix - see below) of node numbers (dot-separated above):


You may return scalars instead of one-element sub-lists:


You may return a matrix (or list of lists) which is padded with zeros so that all rows (sub-lists) have the same length:


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!

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

CJam (15 + 4 = 19 bytes)


Online demo

To reverse,


JavaScript (ES6), 47 + 21 = 68 bytes



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
 b=[])                  Start with an empty sublist

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:

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

Jelly, 10 8 bytes

Forward, 8 6 bytes


Try it online!

Backward, 2 bytes


Try it online!

How it works


ḣ+Ṭ}ð\  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.


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.


Try it online!

Reverse, 2 bytes


Try it online!

  • 3
    \$\begingroup\$ Even more embarrassingly, it's longer than the CJam solution. \$\endgroup\$ May 11 '16 at 8:48
  • \$\begingroup\$ 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.) \$\endgroup\$
    – Neil
    May 11 '16 at 11:35

Pyth, 11 + 2 = 13 bytes






I like this one.

How it works:

               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




->x{x.map &:size}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.