aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorboblied <boblied@gmail.com>2022-12-19 14:56:11 -0600
committerboblied <boblied@gmail.com>2022-12-19 14:56:11 -0600
commit452b8f5905ae8b00cb2d4ab90f7c2b28c3a513c1 (patch)
tree9f26a09047a2d5d16648b3663c2611fb8fae5299
parent10e23b2e5cd4b1d4e9fe98685d05ce495bd2528d (diff)
downloadperlweeklychallenge-club-452b8f5905ae8b00cb2d4ab90f7c2b28c3a513c1.tar.gz
perlweeklychallenge-club-452b8f5905ae8b00cb2d4ab90f7c2b28c3a513c1.tar.bz2
perlweeklychallenge-club-452b8f5905ae8b00cb2d4ab90f7c2b28c3a513c1.zip
Bob Lied W 196 Task 1
-rwxr-xr-xchallenge-196/bob-lied/perl/ch-1.pl80
1 files changed, 80 insertions, 0 deletions
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;
+}
+