aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-053/andrezgz/perl/ch-1.pl72
-rw-r--r--challenge-053/andrezgz/perl/ch-2.pl84
2 files changed, 156 insertions, 0 deletions
diff --git a/challenge-053/andrezgz/perl/ch-1.pl b/challenge-053/andrezgz/perl/ch-1.pl
new file mode 100644
index 0000000000..591f861c71
--- /dev/null
+++ b/challenge-053/andrezgz/perl/ch-1.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-053/
+# Task #1
+#
+# Rotate Matrix
+# 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 ]
+
+use strict;
+use warnings;
+
+use 5.0024; # Postfix dereferencing is stable is v5.24
+
+use constant ROTATION_STEP => 90;
+
+my $matrix = [
+ [ 1, 2, 3 ],
+ [ 4, 5, 6 ],
+ [ 7, 8, 9 ]
+];
+
+# Rotation in degrees
+die 'USAGE: ' . $0 . ' <multiple-of-90>'
+ unless @ARGV == 1 && $ARGV[0] =~ /^\d+$/;
+my $rotation = shift;
+
+# Rotation converted to a number of ROTATION_STEPs
+die 'Rotation must be a multiple of 90, like 90, 180, 270'
+ unless $rotation % ROTATION_STEP == 0;
+$rotation = int($rotation / ROTATION_STEP);
+
+# Rotate matrix clockwise the number of times specified
+$matrix = rotate($matrix) for (1 .. $rotation);
+
+# Rotated matrix is printed
+print '[ ', join(' , ', $matrix->[$_]->@*) , " ]\n"
+ for ( 0 .. $matrix->@* - 1 );
+
+sub rotate {
+ my $input = shift;
+
+ my $output = [];
+ foreach my $r ( 0 .. $input->@* - 1 ){
+ foreach my $c ( 0.. $input->[0]->@* - 1 ){
+ # Each element of the input matrix is appended to the output matrix
+ # at the beginning of the row defined by its column from the input matrix
+ unshift @{$output->[$c]}, $input->[$r]->[$c];
+ }
+ }
+
+ return $output;
+}
+
+__END__
+./ch-1.pl 180
+[ 9 , 8 , 7 ]
+[ 6 , 5 , 4 ]
+[ 3 , 2 , 1 ]
+
+./ch-1.pl 270
+[ 3 , 6 , 9 ]
+[ 2 , 5 , 8 ]
+[ 1 , 4 , 7 ]
diff --git a/challenge-053/andrezgz/perl/ch-2.pl b/challenge-053/andrezgz/perl/ch-2.pl
new file mode 100644
index 0000000000..b1a8ee4c5e
--- /dev/null
+++ b/challenge-053/andrezgz/perl/ch-2.pl
@@ -0,0 +1,84 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-053/
+# Task #2
+#
+# Vowel Strings
+# Write a script to accept an integer 1 <= N <= 5 that would print all possible
+# strings of size N formed by using only vowels (a, e, i, o, u).
+#
+# The string should follow the following rules:
+# 'a' can only be followed by 'e' and 'i'.
+# 'e' can only be followed by 'i'.
+# 'i' can only be followed by 'a', 'e', 'o', and 'u'.
+# 'o' can only be followed by 'a' and 'u'.
+# '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
+
+use strict;
+use warnings;
+
+die "USAGE: $0 <N-string-size>"
+ unless @ARGV == 1 && $ARGV[0] =~ /^[1-5]$/;
+my $n = shift;
+
+my %vowels = (
+ a => ['e','i'],
+ e => ['i'],
+ i => ['a', 'e','o','u'],
+ o => ['a','u'],
+ u => ['o','e']
+);
+
+my @comb = keys %vowels;
+@comb = add_vowel(@comb) for (2..$n);
+print $_.$/ for sort @comb;
+
+sub add_vowel {
+ my @output;
+
+ for my $c (@_) {
+ my $v = substr $c, -1;
+ push @output, map { $c . $_ } @{$vowels{$v}};
+ }
+
+ return @output
+}
+
+__END__
+./ch-2.pl 3
+aeiS
+aia
+aie
+aio
+aiu
+eia
+eie
+eio
+eiu
+iae
+iai
+iei
+ioa
+iou
+iue
+iuo
+oae
+oai
+oue
+ouo
+uei
+uoa
+uou