List rooted trees: Difference between revisions

m
→‎{{header|REXX}}: changed whitespace and some comments.
(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 &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>.
<lang rexx>/*REXX program lists n─node rooted trees (by enumerating all ways of nesting N batobags).*/
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 /*ensureuse enough digs for the next calccalculation.*/
numeric digits max(9, 1 + length( x2b( d2x(2**(nn+1) - 1) ) ) ) /*limit decimal digits.*/
start= 2**nn + (2**nn) % 2 /*calculate the starting decimal number*/
if N==1 then start= 2**1 /*treat the start for unity as special.*/
_= copies('─', 20)"► " /*demonstrative literal for solutions. */
#=0 0 /*count of ways to nest bags (so far). */
$= /*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 right-mostright─most isolated bag ?*/
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) /*save a (possible) duplicious string. */
end /*j*/
say /*separate number─of─ways with a blank.*/