aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-196/zapwai/perl/ch-1.pl28
-rw-r--r--challenge-196/zapwai/perl/ch-2.pl30
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-196/zapwai/perl/ch-1.pl b/challenge-196/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..f7eb7356c5
--- /dev/null
+++ b/challenge-196/zapwai/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+my @list;
+my @testlist = ( [3,1,4,2],
+ [1,2,3,4],
+ [1,3,2,4,6,5],
+ [1,3,4,2],
+ );
+for (@testlist) {
+ @list = @{$_};
+ driver();
+}
+sub driver {
+ print "Input: (".join(",",@list).")\n";
+ print "Output: ";
+
+ for my $i (0 .. $#list - 2) {
+ for my $j ($i + 1 .. $#list -1) {
+ for my $k ($j + 1 .. $#list) {
+ if ( ($list[$i] < $list[$k]) &&
+ ($list[$k] < $list[$j]) ) {
+ print "(", join(",",@list[$i, $j, $k]), ")\n";
+ return;
+ }
+ }
+ }
+ }
+ print "()\n";
+}
diff --git a/challenge-196/zapwai/perl/ch-2.pl b/challenge-196/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..640b34688e
--- /dev/null
+++ b/challenge-196/zapwai/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+my @list1 = (1,3,4,5,7);
+my @list2 = (1,2,3,6,7,9);
+my @list3 = (0,1,2,4,5,6,8,9);
+my @lists = (\@list1, \@list2, \@list3);
+
+for my $ref (@lists) {
+ my @list = @{$ref};
+ print "Input: (".join(",",@list).")\n";
+ print "Output: ";
+
+ my $range_flag = 0;
+ my $comma_flag = 0;
+ for my $index (0 .. $#list) {
+ if ($list[$index + 1] == $list[$index] + 1) {
+ if (!$range_flag) {
+ $range_flag = 1;
+ print ", " if ($comma_flag);
+ print "[$list[$index],";
+ }
+ } else {
+ if ($range_flag) {
+ $range_flag = 0;
+ $comma_flag = 1;
+ print "$list[$index]]";
+ }
+ }
+ }
+ print "\n";
+}