aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-02-14 07:49:43 +0000
committerGitHub <noreply@github.com>2023-02-14 07:49:43 +0000
commit986b633b77c54044dc1679939a79cdb8d3164a4d (patch)
tree3cab6d1660cefbce56a5a1959f2a35e922607d04
parent65facce3c2c2beafc1eb2392a79f82786594c1fc (diff)
parent6732f65edd92a303df28106373a5f9af7660ebba (diff)
downloadperlweeklychallenge-club-986b633b77c54044dc1679939a79cdb8d3164a4d.tar.gz
perlweeklychallenge-club-986b633b77c54044dc1679939a79cdb8d3164a4d.tar.bz2
perlweeklychallenge-club-986b633b77c54044dc1679939a79cdb8d3164a4d.zip
Merge pull request #7570 from robbie-hatley/204
Robbie Hatley's Perl solutions for PWCC 204.
-rw-r--r--challenge-204/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-204/robbie-hatley/perl/ch-1.pl59
-rwxr-xr-xchallenge-204/robbie-hatley/perl/ch-2.pl118
3 files changed, 178 insertions, 0 deletions
diff --git a/challenge-204/robbie-hatley/blog.txt b/challenge-204/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..1548752c4d
--- /dev/null
+++ b/challenge-204/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2023/02/robbie-hatleys-perl-solutions-to-weekly_13.html \ No newline at end of file
diff --git a/challenge-204/robbie-hatley/perl/ch-1.pl b/challenge-204/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..952cbbb867
--- /dev/null
+++ b/challenge-204/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#! /usr/bin/perl
+# Robbie Hatley's Solution to PWCC 204-1
+
+=pod
+
+Task 1: Monotonic Array
+Submitted by: Mohammad S Anwar
+Given an array of integers, write a script that prints 1 if the given array
+is Monotonic, else prints 0.
+Example 1: Input: (1,2,2,3) Output: 1
+Example 2: Input: (1,3,2) Output: 0
+Example 3: Input: (6,5,5,4) Output: 1
+
+=cut
+
+# IO NOTES:
+# NOTE: Input is by either built-in array-of-arrays, or @ARGV.
+# If using @ARGV,the args should be a space-separated sequence of
+# integers, which will be interpreted as being a single array.
+# NOTE: Output is to STDOUT and will be 1 if monotonic, 0 if not.
+
+# PRELIMINARIES:
+use v5.36;
+$"=", ";
+
+# DEFAULT INPUTS:
+my @arrays = ( [1,2,2,3], [1,3,2], [6,5,5,4] );
+
+# NON-DEFAULT INPUTS:
+if (@ARGV) {@arrays = ([@ARGV]);}
+
+# SUBROUTINES:
+
+sub is_mono (@a){
+ my $mono;
+ $mono = 1;
+ for ( my $i = 1 ; $i <= $#a ; ++$i ){
+ $mono &&= ($a[$i-1]<=$a[$i]);} # mono inc?
+ if ( $mono == 1 ) {return 1;}
+ $mono = 1;
+ for ( my $i = 1 ; $i <= $#a ; ++$i ){
+ $mono &&= ($a[$i-1]>=$a[$i]);} # mono dec?
+ if ( $mono == 1 ) {return 2;}
+ return 0;}
+
+# MAIN BODY OF SCRIPT:
+for (@arrays){
+ my @array = @{$_};
+ say '';
+ say "array: (@array)";
+ my $mono = is_mono(@array);
+ if ($mono == 0){
+ say "not monotonic"}
+ elsif ($mono == 1){
+ say "monotonically increasing"}
+ elsif ($mono == 2){
+ say "monotonically decreasing"}
+ else {
+ say "We never had to take any of it seriously, did we?"}} \ No newline at end of file
diff --git a/challenge-204/robbie-hatley/perl/ch-2.pl b/challenge-204/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..5cfad41d99
--- /dev/null
+++ b/challenge-204/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,118 @@
+#! /usr/bin/perl
+# Robbie Hatley's Solution to PWCC 204-2
+
+=pod
+
+Task 2: Reshape Matrix
+Submitted by: Mohammad S Anwar
+Given an m-by-n matrix @m and two integers $r and $c, write a script to
+reshape @m to r-by-c, re-using all of the original elements of @m.
+If this is not possible, just print 0.
+
+Example 1:
+Inputs:
+ old shape:
+ rows = 2
+ cols = 2
+ matrix:
+ 1 2
+ 3 4
+ new shape:
+ rows = 1
+ cols = 4
+Output:
+ 1 2 3 4
+
+Example 2:
+Inputs:
+ old shape:
+ rows = 2
+ cols = 3
+ matrix:
+ 1 2 3
+ 4 5 6
+ new shape:
+ rows = 3
+ cols = 2
+Output:
+ 1 2
+ 3 4
+ 5 6
+
+Example 3:
+Input:
+ old shape:
+ rows = 1
+ cols = 2
+ matrix:
+ 1 2
+ new shape:
+ rows = 3
+ cols = 2
+Output:
+ 0
+
+=cut
+
+# IO NOTES:
+#
+# NOTE: Input is by either built-in array-of-arrays, or @ARGV.
+#
+# If using @ARGV,the args should be a space-separated sequence of
+# integers, which should be the numbers of rows & columns of the
+# original matrix, followed by the elements of the matrix in
+# left-to-right-within-up-to-down order (like reading a book),
+# followed by the numbers of rows & columns desired. For example,
+# to reshape the matrix ([5,8],[1,4]) from 2-by-2 to 4-by-1:
+# ./ch-2.pl 2 2 5 8 1 4 4 1
+#
+# NOTE: Output is to STDOUT and will be the original matrix, followed by
+# the reshaped matrix (or 0 if the matrix can't be reshaped to
+# the given shape).
+
+# PRELIMINARIES:
+use v5.36;
+$"=" ";
+
+# SUBROUTINES:
+
+sub shape {
+ my $r = shift @_;
+ my $c = shift @_;
+ my @flat = @_;
+ if ($r*$c != scalar @flat){
+ return ([0])}
+ my @rows;
+ my $i = 0;
+ for (@flat){
+ $rows[int $i/$c]->[$i%$c]=$_;
+ ++$i}
+ return @rows}
+
+# DEFAULT INPUTS:
+my @arrays =
+(
+ [2,2,1,2,3,4,1,4],
+ [2,3,1,2,3,4,5,6,3,2],
+ [1,2,1,2,3,2]
+);
+
+# NON-DEFAULT INPUTS:
+if (@ARGV) {@arrays = ([@ARGV]);}
+
+# MAIN BODY OF SCRIPT:
+for (@arrays){
+ say '';
+ my @array = @{$_};
+ my $or = shift @array;
+ my $oc = shift @array;
+ my $nc = pop @array;
+ my $nr = pop @array;
+ my @old_rows = shape($or, $oc, @array);
+ my @new_rows = shape($nr, $nc, @array);
+ say "Original matrix ($or rows, $oc cols):";
+ say "@{$_}" for @old_rows;
+ say "Reshaped matrix ($nr rows, $nc cols):";
+ say "@{$_}" for @new_rows;
+ if (1 == @new_rows && 1 == @{$new_rows[0]} && 0 == $new_rows[0]->[0]){
+ say "(Couldn't reshape.)"}} \ No newline at end of file