aboutsummaryrefslogtreecommitdiff
path: root/challenge-202
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-02-04 12:28:20 +0000
committerGitHub <noreply@github.com>2023-02-04 12:28:20 +0000
commit56c5f9c7f47eb19e484ed0e2c8169a36d870a5aa (patch)
treeea764d554fc963858a47b0eef431ebd4513c89b2 /challenge-202
parent7c0437a557a9f69289052696fb26e47a736d14e1 (diff)
parent70dea6e4dc1d3a762eab695418d43d934b8412ac (diff)
downloadperlweeklychallenge-club-56c5f9c7f47eb19e484ed0e2c8169a36d870a5aa.tar.gz
perlweeklychallenge-club-56c5f9c7f47eb19e484ed0e2c8169a36d870a5aa.tar.bz2
perlweeklychallenge-club-56c5f9c7f47eb19e484ed0e2c8169a36d870a5aa.zip
Merge pull request #7507 from robbie-hatley/202
Robbie Hatley's Perl Solutions To The Weekly Challenge #202
Diffstat (limited to 'challenge-202')
-rw-r--r--[-rwxr-xr-x]challenge-202/robbie-hatley/README0
-rw-r--r--challenge-202/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-202/robbie-hatley/perl/ch-1.pl55
-rwxr-xr-xchallenge-202/robbie-hatley/perl/ch-2.pl110
4 files changed, 166 insertions, 0 deletions
diff --git a/challenge-202/robbie-hatley/README b/challenge-202/robbie-hatley/README
index 1b1dc91203..1b1dc91203 100755..100644
--- a/challenge-202/robbie-hatley/README
+++ b/challenge-202/robbie-hatley/README
diff --git a/challenge-202/robbie-hatley/blog.txt b/challenge-202/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..53738017e5
--- /dev/null
+++ b/challenge-202/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2023/01/robbie-hatleys-perl-solutions-to-weekly_31.html \ No newline at end of file
diff --git a/challenge-202/robbie-hatley/perl/ch-1.pl b/challenge-202/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..09ee58ac28
--- /dev/null
+++ b/challenge-202/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#! /usr/bin/perl
+
+# Robbie Hatley's Perl Solutions to The Weekly Challenge #202-1
+
+# DESCRIPTION OF PROBLEM:
+
+=pod
+
+Task 1: Consecutive Odds
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers. Write a script to print 1 if there
+are THREE consecutive odds in the given array otherwise print 0.
+
+Example 1: Input: (1,5,3,6) Output: 1
+Example 2: Input: (2,6,3,5) Output: 0
+Example 3: Input: (1,2,3,4) Output: 0
+Example 4: Input: (2,3,5,7) Output: 1
+
+=cut
+
+# IO NOTES:
+# NOTE: Default input is via built-in array of arrays.
+# Non-default input can be provided through @ARGV.
+# If using @ARGV, arguments should be a space-separated sequence
+# of integers, which will be construed as being a single array.
+#
+# NOTE: Output is to stdout and will be 1 if 3 consecutive odds, else 0.
+
+# PRELIMINARIES:
+use v5.36;
+$,=' ';
+
+# SUBROUTINES:
+sub tco (@a){
+ for (my $i = 0 ; $i <= $#a-2 ; ++$i){
+ if ( !($a[$i+0]%2) ) {$i += 0; next;}
+ if ( !($a[$i+1]%2) ) {$i += 1; next;}
+ if ( !($a[$i+2]%2) ) {$i += 2; next;}
+ return 1;}
+ return 0;}
+
+# DEFAULT INPUT:
+my @arrays = ([1,5,3,6],[2,6,3,5],[1,2,3,4],[2,3,5,7]);
+
+# NON-DEFAULT INPUT:
+if (@ARGV) {@arrays = ([@ARGV]);}
+
+# SCRIPT BODY:
+for (@arrays){
+ my @array = @{$_};
+ my $tco = tco(@array);
+ say '';
+ say "array = (@array)";
+ say "tco = $tco";} \ No newline at end of file
diff --git a/challenge-202/robbie-hatley/perl/ch-2.pl b/challenge-202/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..4a93df3a3d
--- /dev/null
+++ b/challenge-202/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,110 @@
+#! /usr/bin/perl
+
+# Robbie Hatley's Perl Solutions to The Weekly Challenge #202-2
+
+# DESCRIPTION OF PROBLEM:
+
+=pod
+
+Task 2: Widest Valley
+Submitted by: E. Choroba
+
+Given a profile as a list of altitudes, return the leftmost widest valley.
+A valley is defined as a subarray of the profile consisting of two parts:
+the first part is non-increasing and the second part is non-decreasing.
+Either part can be empty.
+
+Example 1: Input: 1, 5, 5, 2, 8 Output: 5, 5, 2, 8
+Example 2: Input: 2, 6, 8, 5 Output: 2, 6, 8
+Example 3: Input: 9, 8, 13, 13, 2, 2, 15, 17 Output: 13, 13, 2, 2, 15, 17
+Example 4: Input: 2, 1, 2, 1, 3 Output: 2, 1, 2
+Example 5: Input: 1, 3, 3, 2, 1, 2, 3, 3, 2 Output: 3, 3, 2, 1, 2, 3, 3
+
+=cut
+
+# IO NOTES:
+#
+# NOTE: Input is via either built-in array of arrays, or @ARGV.
+# If using @ARGV, arguments should be a space-separated sequence of
+# integers, which will be interpreted as being a single array
+#
+# NOTE: Output will be to stdout and will be the contents of each array
+# followed by the left-most widest valley within that array.
+
+# PRELIMINARIES:
+use v5.36;
+$"=", ";
+
+# DEFAULT INPUT:
+my @arrays =
+(
+ [1, 5, 5, 2, 8],
+ [2, 6, 8, 5],
+ [9, 8, 13, 13, 2, 2, 15, 17],
+ [2, 1, 2, 1, 3],
+ [1, 3, 3, 2, 1, 2, 3, 3, 2]
+);
+
+# NON-DEFAULT INPUT:
+if (@ARGV){@arrays=([@ARGV])}
+
+# SCRIPT BODY:
+for (@arrays){
+ my @array = @{$_}; # Current array.
+ my $state = 'L'; # State of valley wall: 'L'=Left, 'R'=Right.
+ my $width = 0; # Width of most-recent valley.
+ my $widbi = 0; # Beginning index of widest valley found so far.
+ my $widest = 0; # Width of widest valley found so-far.
+ my @valley = (); # Left-most widest valley found so far.
+ my $idx = 0; # Elevation index.
+ my $begin = 0; # Beginning index of current valley.
+
+ # For each elevation change, decide what to do, based valley-wall state
+ # and on whether we've reached the last elevation yet:
+ for ( $idx = 1 ; $idx <= $#array ; ++$idx ){
+
+ # If we're on the Left Wall of our current valley, check to see if we
+ # just transitioned to the Right Wall:
+ if ($state eq 'L' && $array[$idx] > $array[$idx-1]){
+ $state = 'R';}
+
+ # If we're on the Right Wall of our current valley, check to see if we
+ # just transitioned to a new Left Wall:
+ if ($state eq 'R' && $array[$idx] < $array[$idx-1]){
+ # How wide was that valley we just left?
+ $width = $idx - $begin;
+
+ # Was that wider than the widest valley we've seen so far?
+ # If so, update widest-valley information accordingly:
+ if ($width > $widest){
+ $widbi = $begin;
+ $widest = $width;}
+
+ # Our old valley ended, so now let's begin a new valley:
+ $begin = $idx;
+
+ # But also include any flat or down-sloping ground to the left
+ # of $idx as also being part of our new valley:
+ while ($begin >= 0 && $array[$begin-1]>=$array[$begin]){--$begin}
+
+ # We're now on the left wall of our new valley:
+ $state = 'L';}
+
+ # If end-of-array cuts off valley, then don't wait for current valley to
+ # terminate by R->L transition, because that can't happen! Instead,
+ # calculate size of current valley now, and update "widest valley"
+ # information if appropriate:
+ if ($idx == $#array){
+ $width = $idx + 1 - $begin;
+ if ($width > $widest){
+ $widbi = $begin;
+ $widest = $width;}}}
+
+ # Set @valley to a slice of @array beginning with "widest beginning index"
+ # "$widbi" and having width "$widest":
+ @valley = @array[$widbi..$widbi+$widest-1];
+
+ # Print results:
+ say '';
+ say "Array of elevations = (@array)";
+ say "Left-most widest valley = (@valley)"}