The weekly challenge 196

This is my solution for the weekly challenge 196.

Task1

Task 1 is to write a script to find out subsequence that respect Pattern 132.
Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j].
 1 #!/usr/bin/perl
 2 use strict;
 3 use warnings;
 4 
 5 # put all examples into an array
 6 my $lists = [
 7    [3, 1, 4, 2],
 8    [1, 2, 3, 4],
 9    [1, 3, 2, 4, 6, 5],
10    [1, 3, 4, 2],
11    [],
12    [1, 2]
13 ];
14 
15 # iterate over all examples and get the results, then print them
16 foreach my $list (@$lists) {
17    my @result = pattern_132(@$list);
18    print "(" . join(", ", @$list) . ") returns (" . join(", ", @result) . ")\n";
19 }
20 
21 # function to get the output for a given list
22 sub pattern_132 {
23    my @list = @_;
24    my $last = $#list; # get index of last element
25    # by iterating $i from 0 to $last, $j from $i+1 to $last and $k from $j+1 to
26    # $last, we have $i<$j<$k automatically.
27    foreach my $i (0..$last) {
28       foreach my $j ($i+1..$last) {
29          foreach my $k ($j+1..$last) {
30             # if both $list[$i] < $list[$k] and $list[$k] < $list[$j] we have
31             # found the first solution, so return it since we're done
32             return  ($list[$i], $list[$j], $list[$k]) if $list[$i] < $list[$k] && $list[$k] < $list[$j];
33          }
34       }
35    }
36    return ();
37 }

Task2

Task 2 is to write a script to find all ranges in an array.
The ranges are all subsequences of two or more contiguous integers.
 1 #!/usr/bin/perl
 2 use strict;
 3 use warnings;
 4 
 5 # put all examples into an array
 6 my @lists = (
 7    [1,3,4,5,7],
 8    [1,2,3,6,7,9],
 9    [0,1,2,4,5,6,8,9]
10 );
11 
12 # iterate over all examples and get the results, then print them
13 foreach my $list (@lists) {
14    my @ranges = ranges(@$list);
15    print "(" . join(", ", @$list) . ") has ranges " . join(", ", @ranges) . "\n";
16 }
17 
18 # function to get the output for a given list
19 sub ranges {
20    my @array = @_;
21    my @result;
22    my $last = undef; # for last element during iteration
23    my $begin = undef; # for last begin of a range
24    foreach my $elem (@array) {
25       if(defined($last)) {
26          if($last+1 == $elem) {
27             # We're in a range.
28             # If $begin wasn't defined, we can set it to $last
29             $begin //= $last;
30             # Since we're in a range, we can set $last to the current element
31             # for the next iteration
32             $last = $elem;
33          } else {
34             # we're no longer in a range, or we weren't in one in the
35             # first place
36             if($begin != $last) {
37                # we were in a range, so put the range into the @result
38                push @result, "[$begin,$last]";
39             }
40             # Since we're now not in a range, let's start from scratch
41             $begin = $last = $elem;
42          }
43       } else {
44          # $last not defined, so first element in the list
45          # initialize both $last and $begin with the current element
46          $begin = $last = $elem;
47       }
48    }
49    if($begin != $last) {
50       # we didn't save the last range in the @result yet, so let's do it now
51       push @result, "[$begin,$last]";
52    }
53    return @result;
54 }