Skip to content

Commit

Permalink
Perl and Raku solutions to Tasks 1 & 2 of the Perl Weekly Challenge m…
Browse files Browse the repository at this point in the history
…anwar#53

On branch branch-for-challenge-053
Changes to be committed:
	new file:   challenge-053/athanasius/perl/ch-1.pl
	new file:   challenge-053/athanasius/perl/ch-2.pl
	new file:   challenge-053/athanasius/raku/ch-1.raku
	new file:   challenge-053/athanasius/raku/ch-2.raku
  • Loading branch information
PerlMonk-Athanasius committed Mar 25, 2020
1 parent d7b97e7 commit 9c30b05
Show file tree
Hide file tree
Showing 4 changed files with 431 additions and 0 deletions.
129 changes: 129 additions & 0 deletions challenge-053/athanasius/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!perl

################################################################################
=comment
Perl Weekly Challenge 053
=========================
Task #1
*Rotate Matrix*
Write a script to rotate the followin[g] 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 ]
=cut
################################################################################

#--------------------------------------#
# Copyright © 2020 PerlMonk Athanasius #
#--------------------------------------#

use strict;
use warnings;
use utf8;
use Const::Fast;

const my @MATRIX =>
(
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ],
);

const my %ROTATIONS =>
(
90 => [
[ [2, 0], [1, 0], [0, 0] ],
[ [2, 1], [1, 1], [0, 1] ],
[ [2, 2], [1, 2], [0, 2] ],
],
180 => [
[ [2, 2], [2, 1], [2, 0] ],
[ [1, 2], [1, 1], [1, 0] ],
[ [0, 2], [0, 1], [0, 0] ],
],
270 => [
[ [0, 2], [1, 2], [2, 2] ],
[ [0, 1], [1, 1], [2, 1] ],
[ [0, 0], [1, 0], [2, 0] ],
],
);

#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\n";
}

#===============================================================================
MAIN:
#===============================================================================
{
printf "Challenge 053, Task #1: Rotate Matrix (Perl)\n\n%s\n",
format_matrix('Original matrix', \@MATRIX);

my $rotation;

for my $degrees (sort { $a <=> $b } keys %ROTATIONS)
{
$rotation = rotate($degrees, \@MATRIX);

print format_matrix("Rotated $degrees°", $rotation), "\n";
}

# 270° rotated a further 90° --> 360° = 0° (the original matrix)

print format_matrix('Rotated 360°', rotate(90, $rotation));
}

#-------------------------------------------------------------------------------
sub format_matrix
#-------------------------------------------------------------------------------
{
my ($title, $matrix) = @_;

my $string = sprintf "%s:\n", $title;
$string .= sprintf "[%s]\n", join( ', ', $matrix->[$_]->@* ) for 0 .. 2;

return $string;
}

#-------------------------------------------------------------------------------
sub rotate
#-------------------------------------------------------------------------------
{
my ($degrees, $old_matrix) = @_;

exists $ROTATIONS{ $degrees }
or die "ERROR: Rotation of $degrees° is not supported\n";

my @new_matrix;

for my $r_new (0 .. 2) # Rows
{
for my $c_new (0 .. 2) # Columns
{
my ($r_old, $c_old) = $ROTATIONS{ $degrees }->[$r_new][$c_new]->@*;

$new_matrix[$r_new]->[$c_new] = $old_matrix->[$r_old][$c_old];
}
}

return \@new_matrix;
}

################################################################################
105 changes: 105 additions & 0 deletions challenge-053/athanasius/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!perl

################################################################################
=comment
Perl Weekly Challenge 053
=========================
Task #2
*Vowel Strings*
Write a script to accept an integer *1 <= N <= 5* that would print all possible
[distinct] strings of size *N* formed by using only vowels (*a, e, i, o, u*).
The string should follow the following rules:
1. 'a' can only be followed by 'e' and 'i'.
2. 'e' can only be followed by 'i'.
3. 'i' can only be followed by 'a', 'e', 'o', and 'u'.
4. 'o' can only be followed by 'a' and 'u'.
5. 'u' can only be followed by 'o' and 'e'.
For example, if the given integer *N = 2* then script should print the following
strings:
ae
ai
ei
ia
io
iu
ie
oa
ou
uo
ue
=cut
################################################################################

#--------------------------------------#
# Copyright © 2020 PerlMonk Athanasius #
#--------------------------------------#

use strict;
use warnings;
use Const::Fast;

const my $MIN_N => 1;
const my $MAX_N => 5;
const my $USAGE => "USAGE: perl $0 <Int:N> where $MIN_N <= N <= $MAX_N\n";
const my %FOLLOWERS =>
(
a => [ qw| e i | ],
e => [ qw| i | ],
i => [ qw| a e o u | ],
o => [ qw| a u | ],
u => [ qw| o e | ],
);

#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\n";
}

#===============================================================================
MAIN:
#===============================================================================
{
print "Challenge 053, Task #2: Vowel Strings (Perl)\n\n";

my $args = scalar @ARGV;
$args == 1 or die "ERROR: $args arguments found\n$USAGE";

my $n = int $ARGV[0];
$MIN_N <= $n && $n <= $MAX_N or die "ERROR: N = $n is out of range\n$USAGE";

my @solution = qw( a e i o u ); # The solution for N = 1

for (2 .. $n)
{
my @temp;

for my $string (@solution)
{
my $last = substr $string, -1;

push @temp, $string . $_ for $FOLLOWERS{ $last }->@*;
}

@solution = @temp;
}

printf "For N = %d, the %d possible distinct vowel strings are:\n %s\n",
$n, scalar @solution, join "\n ", @solution;
}

################################################################################
107 changes: 107 additions & 0 deletions challenge-053/athanasius/raku/ch-1.raku
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use v6;

################################################################################
=begin comment
Perl Weekly Challenge 053
=========================
Task #1
*Rotate Matrix*
Write a script to rotate the followin(g) 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 )
=end comment
################################################################################

#--------------------------------------#
# Copyright © 2020 PerlMonk Athanasius #
#--------------------------------------#

my constant @MATRIX = (1, 2, 3), (4, 5, 6), (7, 8, 9);
my constant %ROTATIONS = 90 => (
( (2, 0), (1, 0), (0, 0) ),
( (2, 1), (1, 1), (0, 1) ),
( (2, 2), (1, 2), (0, 2) ),
),
180 => (
( (2, 2), (2, 1), (2, 0) ),
( (1, 2), (1, 1), (1, 0) ),
( (0, 2), (0, 1), (0, 0) ),
),
270 => (
( (0, 2), (1, 2), (2, 2) ),
( (0, 1), (1, 1), (2, 1) ),
( (0, 0), (1, 0), (2, 0) ),
);

#-------------------------------------------------------------------------------
BEGIN ''.put;
#-------------------------------------------------------------------------------

#===============================================================================
sub MAIN()
#===============================================================================
{
"Challenge 053, Task #1: Rotate Matrix (Raku)\n".put;
format-matrix('Original matrix', @MATRIX).put;

my @rotation;

for %ROTATIONS.keys.sort: { $^a <=> $^b } -> Str $degrees
{
@rotation = rotate($degrees, @MATRIX);

format-matrix("Rotated $degrees degrees", @rotation).put;
}

# 270° rotated a further 90° --> 360° = 0° (the original matrix)

format-matrix('Rotated 360 degrees', rotate('90', @rotation)).print;
}

#-------------------------------------------------------------------------------
sub format-matrix(Str:D $title, @matrix --> Str)
#-------------------------------------------------------------------------------
{
my Str $string = "%s:\n".sprintf: $title;
$string ~= "(%s)\n".sprintf: @matrix[$_].join: ', ' for 0 .. 2;

return $string;
}

#-------------------------------------------------------------------------------
sub rotate(Str:D $degrees, @old-matrix --> Array)
#-------------------------------------------------------------------------------
{
%ROTATIONS{ $degrees }:exists
or die "ERROR: Rotation of $degrees degrees is not supported\n";

my @new-matrix;

for 0 .. 2 -> UInt $row-new
{
for 0 .. 2 -> UInt $col-new
{
my UInt @old = %ROTATIONS{ $degrees }[$row-new][$col-new];

@new-matrix[$row-new][$col-new] = @old-matrix[ @old[0] ][ @old[1] ];
}
}

return @new-matrix;
}

################################################################################
Loading

0 comments on commit 9c30b05

Please sign in to comment.