aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-057/luca-ferrari/raku/ch-1.p633
1 files changed, 22 insertions, 11 deletions
diff --git a/challenge-057/luca-ferrari/raku/ch-1.p6 b/challenge-057/luca-ferrari/raku/ch-1.p6
index 6231a39c47..c135c068b8 100644
--- a/challenge-057/luca-ferrari/raku/ch-1.p6
+++ b/challenge-057/luca-ferrari/raku/ch-1.p6
@@ -28,17 +28,28 @@ sub switch( Node $current-node is rw ) {
}
-
-sub print( $left, $right, $height ) {
- my $message = "%s %s %s".sprintf: " " x $height,
- $left ?? $left.value !! " ",
- $right ?? $right.value !! " ";
-
- $message ~= print( $left.left, $left.right, $height - 1 ) if $left;
- $message ~= print( $right.left, $right.right, $height- 1 ) if $right;
- return $message;
+sub print ( @nodes ) {
+ return if ! @nodes;
+
+ my $spaces = " " x 6 / @nodes.elems;
+ my @children;
+ my $line = "";
+ my $subline = "";
+ loop ( my $i = 0; $i < @nodes.elems; $i++ ) {
+ next if ! @nodes[ $i ];
+ $line ~= $spaces ~ $spaces x $i;
+ $line ~= @nodes[ $i ].value;
+ $subline ~= $spaces ~ $spaces x $i;
+ $subline ~= "/ \\";
+ @children.push: @nodes[ $i ].left, @nodes[ $i ].right;
+ }
+
+ say $line;
+ say $subline;
+ print( @children );
}
+
sub MAIN() {
my Node $root = Node.new( :value( 1 ) );
@@ -50,7 +61,7 @@ sub MAIN() {
$root.right.right = Node.new( :value( 7 ) );
switch( $root );
- # put $root.Str;
- say print $root, Nil, 5;
+
+ print( [ $root ] );
}