List rooted trees: Difference between revisions
Content added Content deleted
(+ D) |
(→{{header|Python}}: Added zkl) |
||
Line 396: | Line 396: | ||
for x in trees(5): print(tostr(x))</lang> |
for x in trees(5): print(tostr(x))</lang> |
||
=={{header|zkl}}== |
|||
{{trans|Python}} |
|||
<lang zkl>fcn bags(n){ |
|||
if(not n) return(T(T(0,""))); |
|||
[n-1 .. 1, -1].pump(List,bags).flatten() : |
|||
bagchain(T(0,""), n-1, _).apply(fcn([(c,s)]){ T(c+1,String("(",s,")")) }) |
|||
} |
|||
fcn bagchain(x,n,bb,start=0){ |
|||
if(not n) return(T(x)); |
|||
out := List(); |
|||
foreach i in ([start..bb.len()-1]){ |
|||
c,s := bb[i]; |
|||
if(c<=n) out.extend(bagchain(L(x[0]+c, x[1]+s), n-c, bb, i)); |
|||
} |
|||
out |
|||
} |
|||
# Maybe this lessens eye strain. Maybe not. |
|||
fcn replace_brackets(s){ |
|||
depth,out := 0,List(); |
|||
foreach c in (s){ |
|||
if(c=="("){ |
|||
out.append("([{"[depth%3]); |
|||
depth += 1; |
|||
}else{ |
|||
depth -= 1; |
|||
out.append(")]}"[depth%3]); |
|||
} |
|||
} |
|||
out.concat() |
|||
} |
|||
foreach x in (bags(5)){ println(replace_brackets(x[1])) }</lang> |
|||
{{out}} |
|||
<pre> |
|||
([{([])}]) |
|||
([{()()}]) |
|||
([{()}{}]) |
|||
([{}{}{}]) |
|||
([{()}][]) |
|||
([{}{}][]) |
|||
([{}][{}]) |
|||
([{}][][]) |
|||
([][][][]) |
|||
</pre> |