Anonymous user
List rooted trees: Difference between revisions
m
→{{header|REXX}}: changed whitespace and some comments.
SqrtNegInf (talk | contribs) (Added Perl example) |
m (→{{header|REXX}}: changed whitespace and some comments.) |
||
Line 1,246:
=={{header|REXX}}==
This REXX version uses (internally) a binary string to represent nodes on a tree (<big>'''0'''</big> is a left parenthesis, <big>'''1'''</big> is a right parenthesis). A <big>'''()'''</big> is translated to a <big>'''O'''</big>.
<lang rexx>/*REXX program lists n─node rooted trees (by enumerating all ways of nesting N
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=5 /*Not specified? Then use the default.*/
if N>5 then do; say N "isn't supported for this program at this time."; exit 13; end
nn= N + N - 1 /*power of 2 that is used for dec start*/
numeric digits 200 /*
numeric digits max(9, 1 + length( x2b( d2x(2**(nn+1) - 1) ) ) ) /*limit decimal digits.*/
start= 2**nn + (2**nn) % 2
if N==1 then start= 2**1 /*treat the start for unity as special.*/
_= copies('─', 20)"► " /*demonstrative literal for solutions. */
#=
$= /*string holds possible duplicious strs*/
do j=start + start//2 to 2**(nn+1)-1 by 2 /*limit the search, smart start and end*/
Line 1,263:
if z\==n then iterate /*Not enough zeroes? Then skip this T.*/
if N>1 then if left(t,N)==right(t,N) then iterate /*left side ≡ right side?*/
if N>2 then if right(t,2)== 10 then iterate /*has a
if N>3 then if right(t,4)== 1100 then iterate /* " " " " "
if N>4 then if right(t,6)==111000 then iterate /* " " " " "
if N>4 then if right(t,6)==110100 then iterate /* " " " " "
if N>4 then if right(t,6)==100100 then iterate /* " " " " "
if wordpos(t, $)\==0 then iterate /*duplicate bag stuffing?*/
say _ changestr('()', translate(t, "()", 10), 'O') /*show a compact display.*/
#= # + 1 /*bump count of ways of nesting bags. */
$= $ translate( reverse(t), 01, 10)
end /*j*/
say /*separate number─of─ways with a blank.*/
|