How to resolve the algorithm List rooted trees step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm List rooted trees step by step in the 11l programming language

Table of Contents

Problem Statement

You came back from grocery shopping.   After putting away all the goods, you are left with a pile of plastic bags, which you want to save for later use, so you take one bag and stuff all the others into it, and throw it under the sink.   In doing so, you realize that there are various ways of nesting the bags, with all bags viewed as identical. If we use a matching pair of parentheses to represent a bag, the ways are: For 1 bag, there's one way: for 2 bags, there's one way: for 3 bags, there are two: for 4 bags, four: Note that because all bags are identical, the two 4-bag strings ((())()) and (()(())) represent the same configuration. It's easy to see that each configuration for n bags represents a n-node rooted tree, where a bag is a tree node, and a bag with its content forms a subtree. The outermost bag is the tree root. Number of configurations for given n is given by OEIS A81.

Write a program that, when given n, enumerates all ways of nesting n bags.   You can use the parentheses notation above, or any tree representation that's unambiguous and preferably intuitive. This task asks for enumeration of trees only; for counting solutions without enumeration, that OEIS page lists various formulas, but that's not encouraged by this task, especially if implementing it would significantly increase code size. As an example output, run 5 bags.   There should be 9 ways.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm List rooted trees step by step in the 11l programming language

Source code in the 11l programming language

F bagchain(x, n, bb, start = 0)
   I n == 0
      R [x]

   [(Int, String)] out
   L(i) start .< bb.len
      V (c, s) = bb[i]
      I c <= n
         out.extend(bagchain((x[0] + c, x[1]‘’s), n - c, bb, i))

   R out

F bags(n)
   I n == 0
      R [(0, ‘’)]

   [(Int, String)] upto
   L(x) (n - 1 .< 0).step(-1)
      upto.extend(bags(x))

   R bagchain((0, ‘’), n - 1, upto).map((c, s) -> (c + 1, ‘(’s‘)’))

F replace_brackets(s)
   V depth = 0
   [String] out
   L(c) s
      I c == ‘(’
         out.append(‘([{’[depth % 3])
         depth++
      E
         depth--
         out.append(‘)]}’[depth % 3])
   R out.join(‘’)

L(x) bags(5)
   print(replace_brackets(x[1]))

  

You may also check:How to resolve the algorithm Constrained random points on a circle step by step in the 11l programming language
You may also check:How to resolve the algorithm Function definition step by step in the Metafont programming language
You may also check:How to resolve the algorithm URL encoding step by step in the C# programming language
You may also check:How to resolve the algorithm Variadic function step by step in the BASIC programming language
You may also check:How to resolve the algorithm String case step by step in the Sed programming language