aboutsummaryrefslogtreecommitdiff
path: root/challenge-196/bob-lied/perl/ch-1.pl
blob: 3bdef2e8fd68433817718a9151c644e218eb839c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
}