aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-196/bob-lied/README4
-rwxr-xr-xchallenge-196/bob-lied/perl/ch-1.pl80
-rwxr-xr-xchallenge-196/bob-lied/perl/ch-2.pl74
3 files changed, 156 insertions, 2 deletions
diff --git a/challenge-196/bob-lied/README b/challenge-196/bob-lied/README
index 08e4900b33..1eebe9fbd4 100644
--- a/challenge-196/bob-lied/README
+++ b/challenge-196/bob-lied/README
@@ -1,3 +1,3 @@
-Solutions to weekly challenge 194 by Bob Lied
+Solutions to weekly challenge 196 by Bob Lied
-https://perlweeklychallenge.org/blog/perl-weekly-challenge-194/
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-196/
diff --git a/challenge-196/bob-lied/perl/ch-1.pl b/challenge-196/bob-lied/perl/ch-1.pl
new file mode 100755
index 0000000000..3bdef2e8fd
--- /dev/null
+++ b/challenge-196/bob-lied/perl/ch-1.pl
@@ -0,0 +1,80 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# Perl Weekly Challenge Week 196, Challenge 1
+#=============================================================================
+# Copyright (c) 2022, Bob Lied
+#=============================================================================
+# You are given a list of integers, @list.
+# Write a script to find out subsequence that respect Pattern 132.
+# Return empty array if none found.
+# Pattern 132 in a sequence (a[i], a[j], a[k])
+# such that i < j < k and 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 susbsequence can be found.
+# Example 3 Input: @list = (1, 3, 2, 4, 6, 5)
+# Output: (1, 3, 2) more than one subsequence found, return first.
+# Example 4 Input: @list = (1, 3, 4, 2)
+# Output: (1, 3, 2)
+#
+#=============================================================================
+
+use v5.36;
+
+use List::Util qw/first/;
+use List::MoreUtils qw/after_incl/;
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
+exit(!runTest()) if $DoTest;
+
+my @list = @ARGV;
+my $answer = find132(\@list);
+say "(", join(", ", $answer->@*), ")";
+exit 0;
+
+sub is132($list, $i1, $i2, $i3)
+{
+ return $list->[$i1] < $list->[$i2]
+ && $list->[$i1] < $list->[$i3]
+ && $list->[$i2] < $list->[$i3];
+}
+
+sub find132($list)
+{
+ return [] if scalar(@$list) < 3;
+
+ while ( my $first = shift @$list )
+ {
+ my @tail = after_incl { $_ > $first } $list->@*;
+ while ( my $middle = shift @tail )
+ {
+ my $last = first { $_ > $first && $_ < $middle } @tail;
+ if ( $last )
+ {
+ return [ $first, $middle, $last ];
+ }
+ }
+ }
+ return [];
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( find132( [ 3,2,1,0 ] ), [ ], "Descending");
+ is( find132( [ 1,3,2 ] ), [1,3,2], "Obvious");
+ is( find132( [ 3,1,4,2 ] ), [1,4,2], "Example 1");
+ is( find132( [ 1,2,3,4 ] ), [ ], "Example 2");
+ is( find132( [ 1,3,2,4,6,5 ] ), [1,3,2], "Example 3");
+ is( find132( [ 1,3,4,2 ] ), [1,3,2], "Example 4");
+
+ done_testing;
+}
+
diff --git a/challenge-196/bob-lied/perl/ch-2.pl b/challenge-196/bob-lied/perl/ch-2.pl
new file mode 100755
index 0000000000..08eccfd031
--- /dev/null
+++ b/challenge-196/bob-lied/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# Perl Weekly Challenge Week 196 Task 2 Range List
+#=============================================================================
+# Copyright (c) 2022, Bob Lied
+#=============================================================================
+# You are given a sorted unique integer array, @array.
+# Write a script to find all possible Number Range i.e [x, y] represent range
+# all integers from x and y (both inclusive).
+# Each subsequence of two or more contiguous integers
+# Example 1 Input: @array = (1,3,4,5,7)
+# Output: [3,5]
+# Example 2 Input: @array = (1,2,3,6,7,9)
+# Output: [1,3], [6,7]
+# Example 3 Input: @array = (0,1,2,4,5,6,8,9)
+# Output: [0,2], [4,6], [8,9]
+#
+#=============================================================================
+
+use v5.36;
+
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
+exit(!runTest()) if $DoTest;
+
+for my $arg ( @ARGV )
+{
+ $arg =~ tr/[(),/ /;
+ my @array = split(' ', $arg);
+
+ my $result = findRun(\@array);
+
+ say join(', ', map { "[@$_]" } $result->@* );
+}
+
+sub findRun($list)
+{
+ my $len = scalar(@$list);
+ return [[]] if $len < 2;
+
+ my @run = ();
+ my ($i, $j) = (0,1);
+ while ( $i < $len )
+ {
+ $j = $i + 1;
+ while ( $j < $len && $list->[$j] == ($list->[$j-1]+1) )
+ {
+ $j++;
+ }
+ if ( $j - $i > 1 )
+ {
+ push @run, [ $list->[$i], $list->[$j-1] ];
+ }
+ $i = $j;
+ }
+ return \@run;
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( findRun( [ 1,3,4,5,7 ] ), [ [3,5] ], "Example 1");
+ is( findRun( [ 1,2,3,6,7,9 ] ), [ [1,3], [6,7] ], "Example 2");
+ is( findRun( [ 0,1,2,4,5,6,8,9 ] ), [ [0,2], [4,6], [8,9] ], "Example 3");
+
+ done_testing;
+}
+