diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2020-04-20 14:11:49 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2020-04-20 14:11:49 +0200 |
| commit | 24f991a965064f04e964e93dfd360756e1927602 (patch) | |
| tree | 3d18a5259c647b0c4ad61088248bf50ba39d553c /challenge-057 | |
| parent | c86949ce7a84d582bcce84683a8f03b6221f5d35 (diff) | |
| download | perlweeklychallenge-club-24f991a965064f04e964e93dfd360756e1927602.tar.gz perlweeklychallenge-club-24f991a965064f04e964e93dfd360756e1927602.tar.bz2 perlweeklychallenge-club-24f991a965064f04e964e93dfd360756e1927602.zip | |
Task 1 done.
Diffstat (limited to 'challenge-057')
| -rw-r--r-- | challenge-057/luca-ferrari/raku/ch-1.p6 | 33 |
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 ] ); } |
