aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPip <Pip@CPAN.Org>2022-12-22 15:50:51 -0600
committerPip <Pip@CPAN.Org>2022-12-22 15:50:51 -0600
commit11e8918c167e9b31b77252a50b62ffd6129b1395 (patch)
treeff32a57798e91616b53758abb02d5dd836c8ccb8
parent49e34ffb1226c5d931cfc8366c2bafc9836cf976 (diff)
downloadperlweeklychallenge-club-11e8918c167e9b31b77252a50b62ffd6129b1395.tar.gz
perlweeklychallenge-club-11e8918c167e9b31b77252a50b62ffd6129b1395.tar.bz2
perlweeklychallenge-club-11e8918c167e9b31b77252a50b62ffd6129b1395.zip
Pip Stuart's submission for challenge-196;
-rw-r--r--challenge-196/pip/perl/ch-1.pl39
-rw-r--r--challenge-196/pip/perl/ch-2.pl30
2 files changed, 69 insertions, 0 deletions
diff --git a/challenge-196/pip/perl/ch-1.pl b/challenge-196/pip/perl/ch-1.pl
new file mode 100644
index 0000000000..8a936b3e6b
--- /dev/null
+++ b/challenge-196/pip/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+# Given a list of ints, find subsequence that respects Pattern 132. Return empty array if none found. i < j < k && a[i] < a[k] < a[j];
+# Example 1:
+# Input : @list = (3, 1, 4, 2)
+# Output: (1, 4, 2) respect the Pattern 132.
+# Example 2:
+# Input : @list = (1, 2, 3, 4)
+# Output: () since no subsequence can be found.
+# Example 3:
+# Input : @list = (1, 3, 2, 4, 6, 5)
+# Output: (1, 3, 2) if more than one subsequence found then return the first.
+# Example 4:
+# Input : @list = (1, 3, 4, 2)
+# Output: (1, 3, 2)
+use strict;use warnings;use utf8;use v5.10;my $d8VS='MCMLB43P';
+sub P132 {my @ilst=@_; # Last week, I treated output comments as to be printed.
+ print '(' . sprintf("%-16s",join(', ',@ilst)) . ') => '; # This week, I just print input.
+ for my $i ( 0..($#ilst-2)){
+ for my $j (($i+1)..($#ilst-1)){
+ if ($ilst[$i] < $ilst[$j]){
+ for my $k (($j+1).. $#ilst ){
+ if($ilst[$i] < $ilst[$k] && $ilst[$k] < $ilst[$j]){
+ say "($ilst[$i], $ilst[$j], $ilst[$k]);";
+ return($ilst[$i], $ilst[$j], $ilst[$k]);
+ }
+ }
+ }
+ }
+ } say "();";
+ return();
+}
+if(@ARGV){
+ P132(@ARGV);
+}else{
+ P132(3, 1, 4, 2);
+ P132(1, 2, 3, 4);
+ P132(1, 3, 2, 4, 6, 5);
+ P132(1, 3, 4, 2);
+}
diff --git a/challenge-196/pip/perl/ch-2.pl b/challenge-196/pip/perl/ch-2.pl
new file mode 100644
index 0000000000..89d582e97b
--- /dev/null
+++ b/challenge-196/pip/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+# Given a sorted unique integer array, find all possible Number Range i.e., [x,y] representing range from x and y (both inclusive).
+# Example 1: Each subsequence of 2 or more contiguous integers
+# In-put: @array = (1,3,4,5,7)
+# Output: [3,5]
+# Example 2:
+# In-put: @array = (1,2,3,6,7,9)
+# Output: [1,3], [6,7]
+# Example 3:
+# In-put: @array = (0,1,2,4,5,6,8,9)
+# Output: [0,2], [4,6], [8,9]
+use strict;use warnings;use utf8;use v5.10;my $d8VS='MCMLCLen';
+sub NumR {my @iary=@_;my $mult=0;my @oary=();my $i=0; # Last week, I treated output comments as to be printed.
+ print '(' . sprintf("%-15s",join(',',@iary)) . ') => '; # This week, I just print input.
+ while ($i < $#iary){my $strt=$i;
+ while($i < $#iary && $iary[$i+1] == ($iary[$i] + 1)){$i++;}
+ if($strt != $i){
+ print ', ' if($mult);
+ print "[$iary[$strt],$iary[$i]]";$mult=1;push(@oary,[$iary[$strt],$iary[$i]]);
+ } $i++;
+ } say ";";
+ return(@oary);
+}
+if(@ARGV){
+ NumR(@ARGV);
+}else{
+ NumR(1,3,4,5,7);
+ NumR(1,2,3,6,7,9);
+ NumR(0,1,2,4,5,6,8,9);
+}