From 211ec4c3a7a465a0a0148748088cb7f1a503d3af Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 23 Mar 2020 08:39:26 +0100 Subject: Task1 done. --- challenge-053/luca-ferrari/raku/ch-1.p6 | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 challenge-053/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-053/luca-ferrari/raku/ch-1.p6 b/challenge-053/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..1ee72eb589 --- /dev/null +++ b/challenge-053/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,69 @@ +#!env raku +# +# Perl Weekly Challenge 53 +# +# +# Task 1 +# +# Write a script to rotate the followin matrix by given 90/180/270 degrees clockwise. +# +# [ 1, 2, 3 ] +# [ 4, 5, 6 ] +# [ 7, 8, 9 ] +# +# For example, if you rotate by 90 degrees then expected result should be like below +# +# [ 7, 4, 1 ] +# [ 8, 5, 2 ] +# [ 9, 6, 3 ] +# +# +# +# Example of invocation +# % raku ch-1.p6 180 +# Rotation is 180 +# Original matrix is +# | 1 | 2 | 3 | +# | 4 | 5 | 6 | +# | 7 | 8 | 9 | +# Rotated matrix is +# | 9 | 8 | 7 | +# | 6 | 5 | 4 | +# | 3 | 2 | 1 | +# + + +sub MAIN( Int:D $rotation where { $rotation == any( 90, 180, 270 ) } = 90 ) { + + say "Rotation is $rotation"; + + my @matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]; + say "Original matrix is "; + say " | " ~ $_.join( " | " ) ~ " | " for @matrix; + + my @rotated-matrix; + + # do a loop for all the rotation + for 1 .. $rotation / 90 { + + # the matrix is a square matrix, move on the columns + # to get the rotated row + for 0 .. @matrix.elems - 1 -> $current-column { + my @rotated-row; + for 0 .. @matrix.elems - 1 { + @rotated-row.push: @matrix[ @matrix.elems - 1 - $_ ][ $current-column ] + } + + # push the rotated row to the new matrix + @rotated-matrix.push: [ @rotated-row ]; + } + + # switch the matrix, in the case we need to loop further + @matrix = @rotated-matrix; + @rotated-matrix = []; + } + + say "Rotated matrix is "; + say " | " ~ $_.join( " | " ) ~ " | " for @matrix; + +} -- cgit