 Perl: the Markov chain saw PerlMonks

by hdb (Monsignor)
 on Mar 05, 2013 at 11:57 UTC Need Help??

If I ask Box 1 or Box 2 will you flash red? If I ask Box 1 will you flash green?
```                     Box
Response  1          2        3
Red/Green  yes/no    Red/Green  Red/Red  Green/Red
no/yes    Red/Green  Red/Red  Green/Red
DumpTree code:
```use strict;
use warnings;

use constant { LEFT => 0, RIGHT => 1 };

sub prepare {
my( \$tree, \$w, \$lr, \$level, \$col, \$canvas, \$chars ) = @_;
if( ref(\$tree) ) {
my( \$lcol, \$rcol ) = map {
\$\$col += \$w*\$_;
prepare( \$tree->[\$_], \$w, \$_, \$level+1, \$col,
+\$canvas, \$chars )
} LEFT, RIGHT;
\$canvas->[\$level  ]->[\$lcol] = \$\$chars[LEFT];
\$canvas->[\$level-1]->[\$_   ] = '_' for \$lcol+1..\$rcol-
+1;
\$canvas->[\$level  ]->[\$rcol] = \$\$chars[RIGHT];
return \$lr ? \$lcol : \$rcol;
} else {
\$canvas->[\$level++]->[\$\$col] = \$_ for split //, \$tree;
+
return \$\$col;
}
}

sub dumpTree {
my (\$col, @canvas ) = (1);
my \$width = \$_ > 0 ? 1+\$_ : 1;
my \$chars = \$_ ? [ '|', '|' ] : [ '/', '\\' ];
prepare( \$_, \$width, RIGHT, 1, \\$col, \@canvas, \$chars );
return join( '', map { join( '', map { \$_ // ' ' } @\$_, "\n" )
+ } @canvas );
}

sub randomTree {
my @t = @_;
my \$r;
\$r = int( rand \$#t ), splice @t, \$r, 2, [ @t[ \$r, \$r+1 ]] whil
+e @t > 1;
return \$t;
}

my @t1 = ( 'a'..'z' );
my @t2 = qw( Alpha Beta Gamma Delta Epsilon Zeta Eta Theta Iota Kappa
+Lambda
Mu Nu Xi Omicron Pi Rho Sigma Tau Upsilon Phi Chi Psi Ome
+ga);

print dumpTree( randomTree( @t1 ), 1 );
print dumpTree( randomTree( @t2 ), 3, 1 );
9833 ♩
9834 ♪
9835 ♫
9836 ♬
9837 ♭
9838 ♮
9839 ♯

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2020-10-25 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favourite web site is:

Results (248 votes). Check out past polls.

Notices?