List rooted trees: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|jq}}: subsecond times)
m (syntax highlighting fixup automation)
Line 37:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F bagchain(x, n, bb, start = 0)
I n == 0
R [x]
Line 72:
 
L(x) bags(5)
print(replace_brackets(x[1]))</langsyntaxhighlight>
 
{{out}}
Line 89:
=={{header|C}}==
Trees are represented by integers. When written out in binary with LSB first, 1 is opening bracket and 0 is closing.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 172:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 190:
=={{header|C++}}==
{{trans|Java}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 280:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Number of 5-trees: 9
Line 295:
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio, std.conv;
 
alias Tree = ulong,
Line 380:
stderr.writefln("Number of %d-trees: %u", n, offset[n + 1] - offset[n]);
listTees(n, offset, list);
}</langsyntaxhighlight>
{{out}}
<pre>Number of 5-trees: 9
Line 395:
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 494:
fmt.Fprintf(os.Stderr, "Number of %d-trees: %d\n", n, offset[n+1]-offset[n])
listTrees(uint(n))
}</langsyntaxhighlight>
 
{{out}}
Line 513:
=={{header|Haskell}}==
There probably is a nicer way than the following--
<langsyntaxhighlight lang="haskell">-- break n down into sum of smaller integers
parts :: Int -> [[(Int, Int)]]
parts n = f n 1
Line 546:
 
main :: IO ()
main = mapM_ putStrLn $ trees 5</langsyntaxhighlight>
{{out}}
<pre>
Line 562:
A variant expressed in terms of Data.Tree
 
<langsyntaxhighlight lang="haskell">import Data.List (foldl', nub, sortOn) --' strict variant of foldl
import Data.Ord (comparing)
import Data.Tree (Tree (..), foldTree)
Line 608:
forest
| root == x = nest <> [Node i []]
| otherwise = (`go` (i, x)) <$> nest</langsyntaxhighlight>
{{Out}}
<pre>(()()()())
Line 624:
Support code:
 
<langsyntaxhighlight Jlang="j">root=: 1 1 $ _
incr=: ,/@(,"1 0/ i.@{:@$)
 
boxed=: $:&0 :(<@\:~@([ $:^:(0 < #@]) I.@:=))"1 1 0</langsyntaxhighlight>
 
Task:
Line 654:
So, for example, here is what some intermediate results would look like for the four bag case:
 
<langsyntaxhighlight Jlang="j"> incr^:3 root
_ 0 0 0
_ 0 0 1
Line 660:
_ 0 1 0
_ 0 1 1
_ 0 1 2</langsyntaxhighlight>
 
Each row represents a bag with another three bags stuffed into it. Each column represents a bag, and each index is the column of the bag that it is stuffed into. (The first bag isn't stuffed into another bag.)
Line 666:
But some of these are equivalent, we can see that if we use our parenthesis notation and think about how they could be rearranged:
 
<langsyntaxhighlight Jlang="j"> disp=: ('(' , ')' ,~ [: ; [ <@disp"1 0^:(0 < #@]) I.@:=) {.
disp incr^:3 root
(()()())
Line 673:
((())())
((()()))
(((())))</langsyntaxhighlight>
 
But that's not a convenient way of finding the all of the duplicates. So we use a boxed representation - with all boxes at each level in a canonical order (fullest first) - and that makes the duplicates obvious:
Line 690:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.ArrayList;
import java.util.List;
 
Line 780:
test(5);
}
}</langsyntaxhighlight>
{{out}}
<pre>Number of 5-trees: 9
Line 796:
===ES6===
Composing a solution from generic functions.
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,017:
// MAIN ---
return main()
})();</langsyntaxhighlight>
{{Out}}
<pre>(()()()())
Line 1,044:
(*) Subsecond times in the case of the C implementation of jq.
 
<langsyntaxhighlight lang="jq"># add one bag somewhere
def addbag:
def sortmap: map(sortmap) | sort;
Line 1,075:
"",
count_rootedtrees(17)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,109:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">bags(n,cache="") = n < 1 ? [(0, "")] :
[(c + 1, "(" * s * ")") for (c, s) in bagchain((0, ""), n - 1,
n < 2 ? [] : reduce(append!, [bags(x) for x in n-1:-1:1]))]
Line 1,120:
println(bag[2])
end
</langsyntaxhighlight>{{out}}
<pre>
((((()))))
Line 1,135:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
typealias Tree = Long
Line 1,216:
println("Number of $n-trees: ${offset[n + 1] - offset[n]}")
listTrees(n)
}</langsyntaxhighlight>
 
{{out}}
Line 1,234:
=={{header|Lua}}==
{{trans|Java}}
<langsyntaxhighlight lang="lua">tree_list = {}
offset = {}
 
Line 1,317:
 
init()
test(5)</langsyntaxhighlight>
{{out}}
<pre>Number of 5-trees: 9
Line 1,332:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
The following defines functions which create a nest of functions, bags wrapped inside a main bag which are stored inside a "cabinet".
<langsyntaxhighlight Mathematicalang="mathematica">Addbags[configs_List] :=
DeleteDuplicates[
Map[LexicographicSort,
Line 1,340:
Addbag[config_, pos_] :=
ReplacePart[config, pos -> Append[Extract[config, pos], bag[]]]
With[{n = 5}, Nest[Addbags, {cabinet[bag[]]}, n - 1] // Column]</langsyntaxhighlight>
The output can be viewed as a tree graph by replacing the last line with <Lang Mathematica>With[{n = 5}, TreeForm /@ Nest[Addbags, {cabinet[bag[]]}, n - 1]]</langsyntaxhighlight>
 
{{out}}
Line 1,356:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import os, strformat, strutils
 
type
Line 1,443:
trees.make(n)
echo &"Number of {n}-trees: {trees.offsets[n + 1] - trees.offsets[n]}"
trees.print(n)</langsyntaxhighlight>
 
{{out}}
Line 1,460:
=={{header|Perl}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,500:
}
 
say replace_brackets $$_[1] for bags(5);</langsyntaxhighlight>
{{out}}
<pre>([{([])}])
Line 1,514:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
Line 1,589:
<span style="color: #000000;">main</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,634:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def bags(n,cache={}):
if not n: return [(0, "")]
 
Line 1,661:
return "".join(out)
 
for x in bags(5): print(replace_brackets(x[1]))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,676:
 
Another method by incrementing subtrees:
<langsyntaxhighlight lang="python">treeid = {(): 0}
 
'''
Line 1,725:
def tostr(x): return "(" + "".join(map(tostr, x)) + ")"
 
for x in trees(5): print(tostr(x))</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require racket/splicing data/order)
 
Line 1,773:
(for-each displayln (LRTs 5))
(equal? (map (compose length LRTs) (range 1 (add1 13)))
'(1 1 2 4 9 20 48 115 286 719 1842 4766 12486))) ;; https://oeis.org/A000081</langsyntaxhighlight>
 
{{out}}
Line 1,791:
Bags are represented by Raku type [http://doc.raku.org/type/Bag <code>Bag</code>].
 
<syntaxhighlight lang="raku" perl6line>use v6;
 
multi expand-tree ( Bag $tree ) {
Line 1,811:
.say
};
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,828:
=={{header|REXX}}==
This REXX version uses (internally) a binary string to represent nodes on a tree &nbsp; (<big>'''0'''</big> &nbsp; is a left parenthesis, &nbsp; <big>'''1'''</big> &nbsp; is a right parenthesis). &nbsp; A &nbsp; <big>'''()'''</big> &nbsp; is translated to a &nbsp; <big>'''O'''</big>.
<langsyntaxhighlight lang="rexx">/*REXX program lists n─node rooted trees (by enumerating all ways of nesting N bags).*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=5 /*Not specified? Then use the default.*/
Line 1,856:
end /*j*/
say /*separate number─of─ways with a blank.*/
say # ' is the number of ways to nest' n "bags." /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,873:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : List rooted trees
 
Line 1,944:
ok
end
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,963:
=={{header|Ruby}}==
{{trans|Java}}
<langsyntaxhighlight lang="ruby">TREE_LIST = []
OFFSET = []
 
Line 2,041:
end
 
test(5)</langsyntaxhighlight>
{{out}}
<pre>Number of 5-trees: 9
Line 2,056:
=={{header|Sidef}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">func bagchain(x, n, bb, start=0) {
n || return [x]
 
Line 2,094:
for x in (bags(5)) {
say replace_brackets(x[1])
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,111:
{{trans|Kotlin}}
{{libheader|Wren-long}}
<langsyntaxhighlight lang="ecmascript">import "/long" for ULong
import "os" for Process
 
Line 2,181:
makeTrees.call(n)
System.print("Number of %(n)-trees: %(offset[n + 1] - offset[n])")
listTrees.call(n)</langsyntaxhighlight>
 
{{out}}
Line 2,200:
Note that "( () (()) () )" the same as "( (()) () () )"
{{trans|Python}}
<langsyntaxhighlight lang="zkl">fcn bags(n){
if(not n) return(T(T(0,"")));
 
Line 2,232:
foreach x in (bags(5)){ println(replace_brackets(x[1])) }
println("or");
b:=bags(5); b.apply("get",1).println(b.len());</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits