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
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
16 foreach my $list (@$lists) {
17 my @result = pattern_132(@$list);
18 print "(" . join(", ", @$list) . ") returns (" . join(", ", @result) . ")\n";
19 }
20
21
22 sub pattern_132 {
23 my @list = @_;
24 my $last = $#list;
25
26
27 foreach my $i (0..$last) {
28 foreach my $j ($i+1..$last) {
29 foreach my $k ($j+1..$last) {
30
31
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
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
13 foreach my $list (@lists) {
14 my @ranges = ranges(@$list);
15 print "(" . join(", ", @$list) . ") has ranges " . join(", ", @ranges) . "\n";
16 }
17
18
19 sub ranges {
20 my @array = @_;
21 my @result;
22 my $last = undef;
23 my $begin = undef;
24 foreach my $elem (@array) {
25 if(defined($last)) {
26 if($last+1 == $elem) {
27
28
29 $begin //= $last;
30
31
32 $last = $elem;
33 } else {
34
35
36 if($begin != $last) {
37
38 push @result, "[$begin,$last]";
39 }
40
41 $begin = $last = $elem;
42 }
43 } else {
44
45
46 $begin = $last = $elem;
47 }
48 }
49 if($begin != $last) {
50
51 push @result, "[$begin,$last]";
52 }
53 return @result;
54 }