Download the raw code.
#!/usr/bin/env perl6
# This is perl6 version 2012.01 built on parrot 3.11.0 revision 0
sub shorten ( @arr is copy ) {
my $pos = @arr.end;
my $sum = 0;
loop ( ; $pos >= 0; $pos ) {
my $valueatpos = @arr[$pos];
$sum += $valueatpos;
if $valueatpos > 1 {
my $newvalue = $valueatpos  1;
@arr.splice( $pos );
@arr[ $pos ] = $newvalue;
$sum = $newvalue;
while $sum {
$newvalue = $newvalue < $sum ?? $newvalue !! $sum;
@arr.push( $newvalue );
$sum = $newvalue;
}
return @arr;
}
}
return;
}
sub getbranchlens( $remains is copy ) {
my @res;
my @row = $remains;
while @row.elems {
my @cp = @row;
@res.push( @cp.item );
@row = shorten( @row );
}
return @res;
}
sub append( @arr, @brr, $base ) {
my @res;
for @arr > $a {
for @brr > $b {
@res.push( ( $a, $base, $b ).join( "" ) );
}
}
if @arr.elems == 0 {
for @brr > $b {
@res.push( "$b" );
}
}
return @res;
}
sub generaterootedtrees ( $startat, $grow ) {
if $grow == 0 {
return [ "$startat" ];
}
if $grow == 1 {
my @vals = $startat, $startat + 1, $startat;
return [ @vals.join( "" ) ];
}
my @arr = getbranchlens( $grow );
my @trees;
for @arr > @ar {
my @growingtrees;
my $growat = $startat + 1;
for @ar > $a {
my @growth = generaterootedtrees( $growat, $a  1 );
@growingtrees = append( @growingtrees, @growth, $startat );
$growat += $a;
}
for @growingtrees {
@trees.push( ($startat, $_, $startat).join( "" ) );
}
}
return @trees;
}
sub filtertrees ( @arr ) { # this is a joke :)
my %seen;
my @res;
for @arr > $a {
my %cnt = 1 => 1;
my @nodes = $a.split( '' );
for @nodes > $n {
%cnt{ $n } += 2;
}
%cnt{ @nodes.end } = 1;
my $k = %cnt.values.sort.join( "" );
if not %seen.exists( $k ) {
@res.push( $a );
%seen{ $k } = 1;
}
}
return @res;
}
sub MAIN ( Int $n ) {
if $n <= 0 {
exit 0;
}
elsif $n == 1 {
say 1; exit 0;
};
my @rootedtrees = generaterootedtrees( 2, $n.Int  2 );
my @unrootedtrees = filtertrees( @rootedtrees );
for @unrootedtrees > $t {
say "1$t";
}
}
The program fails for N = 6
:
$ nom solutions/t5/az5112 6
1234565432
1234546432
1234353632
1234353262
1232425262
That corresponds to these trees:
oooooo
ooooo

o
o

oooo

o
oooo
 
o o
o o
\ /
ooo

o
But there is a distinct sixth one, 1234543632, which it should also have emitted:
ooooo

o
The problem, in short, is that the filtertrees
subroutine uses an
equivalence relation which can't distinguish between this omitted tree and the
second one. The equivalence relation is essentially "how many times was each
node visited", which happens to be the same for these two trees.
Nothing in particular to say here.
The code is nice, and clear. It doesn't get fancy.
The MAIN
method is nice and clear because all the heavy lifting is done in
routines that were defined before that.
This line seems confused beyond the reviewer's ability to explain it:
%cnt{ @nodes.end } = 1;
Not applicable as the program is incorrect.
The code leans more towards "generic procedural code" than towards exploiting the strengths of Perl 6 in particular.
On line 21 there's a missed opportunity to use min=
.
These lines
my $pos = @arr.end;
loop ( ; $pos >= 0; $pos ) {
probably more idiomatically written as
for (^@arr).reverse > $pos {
The code lets itself breathe without being verbose.