1
\$\begingroup\$

In a fictional document management system, input documents are structured with weighted headings. Lowest weighted headings are the most important.

This is a sample document:

H1 All About Birds

H2 Kinds of Birds

H3 The Finch

H3 The Swan

H2 Habitats

H3 Wetlands

From this document we would like to produce an outline using nested ordered lists in HTML, which would look like this when rendered:

  1. All About Birds
    1. Kinds of Birds
      1. The Finch
      2. The Swan
    2. Habitats
      1. Wetlands

Your code only needs to take the input list and transform it into the expected data structure, which will be printed.

\$\endgroup\$
  • 1
    \$\begingroup\$ When you say "using nested ordered lists in HTML" do you mean we need to produce HTML? If so could you define its form for us? (Or may we just indent using a space (or a tab?)?) \$\endgroup\$ – Jonathan Allan May 11 '19 at 20:37
  • \$\begingroup\$ Can the titles be assumed to only contain letters and spaces? \$\endgroup\$ – ArBo May 11 '19 at 21:06
  • 2
    \$\begingroup\$ Hi and welcome to codegolf! To avoid having to answer questions like those you received, please note that we have a sandbox for proposed challenges which allows you to receive feedback before posting. As is you are missing a few important details, like those already asked about, and things like what the highest header level there will be (or if it is indefinite). \$\endgroup\$ – FryAmTheEggman May 11 '19 at 21:26
  • 1
    \$\begingroup\$ May the level be greater than \$9\$? \$\endgroup\$ – Arnauld May 11 '19 at 23:06
  • 2
    \$\begingroup\$ Are we guaranteed to always have level \$L+1\$ between levels \$L\$ and \$L+2\$? Are we supposed to return valid HTML? (with all trailing </ol> tags) \$\endgroup\$ – Arnauld May 11 '19 at 23:35
0
\$\begingroup\$

Jelly, 53 50 bytes

Ḋœṡ¥€⁶ZµḢVØ0jI>0ẋ"AƲị“<ol>“</ol>”żṪ“<li>“</li>”jⱮƊ

Try it online!

A monadic link taking a list of strings as input and outputting an HTML fragment with nested ordered lists. Should handle arbitrarily deep nesting of lists and return a syntactically valid HTML fragment.

Example:

H1 All About Birds
H2 Kinds of Birds
H3 The Finch
H3 The Swan
H2 Habitats
H3 Wetlands
H4 Types of wetlands
H5 Very wet wetland
H3 Coast
H1 All about fish
H2 Sharks

yields:

<ol><li>All About Birds</li><ol><li>Kinds of Birds</li><ol><li>The Finch</li><li>The Swan</li></ol><li>Habitats</li><ol><li>Wetlands</li><ol><li>Types of wetlands</li><ol><li>Very wet wetland</li></ol></ol><li>Coast</li></ol></ol><li>All about fish</li><ol><li>Sharks</li></ol></ol>
  1. All About Birds
    1. Kinds of Birds
      1. The Finch
      2. The Swan
    2. Habitats
      1. Wetlands
        1. Types of wetlands
          1. Very wet wetland
      2. Coast
  2. All about fish
    1. Sharks
\$\endgroup\$

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