aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-196/e-choroba/perl/ch-1.pl27
-rwxr-xr-xchallenge-196/e-choroba/perl/ch-2.pl27
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-196/e-choroba/perl/ch-1.pl b/challenge-196/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..1070c69fb3
--- /dev/null
+++ b/challenge-196/e-choroba/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+sub pattern_132 ($list) {
+ for my $i (0 .. $#$list - 2) {
+ for my $j ($i + 1 .. $#$list - 1) {
+ for my $k ($j + 1 .. $#$list) {
+ return [@$list[$i, $j, $k]]
+ if $list->[$i] < $list->[$k]
+ && $list->[$k] < $list->[$j];
+ }
+ }
+ }
+ return []
+}
+
+
+use Test2::V0; plan 4 + 1;
+
+is pattern_132([3, 1, 4, 2]), [1, 4, 2], 'Example 1';
+is pattern_132([1, 2, 3, 4]), [], 'Example 2';
+is pattern_132([1, 3, 2, 4, 6, 5]), [1, 3, 2], 'Example 3';
+is pattern_132([1, 3, 4, 2]), [1, 3, 2], 'Example 4';
+
+is pattern_132([1, 5, 7, 4]), [1, 5, 4], 'Not 1 7 4';
diff --git a/challenge-196/e-choroba/perl/ch-2.pl b/challenge-196/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..49cc8f6f5d
--- /dev/null
+++ b/challenge-196/e-choroba/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+sub range_list ($arr) {
+ my @ranges = ([shift @$arr]);
+ for my $r (@$arr, $ranges[0][0] - 1) {
+ if ($r == $ranges[-1][-1] + 1) {
+ $ranges[-1][1] = $r;
+ } else {
+ my $is_range = 2 == @{ $ranges[-1] };
+ splice @ranges, $is_range ? $#ranges + 1 : $#ranges, 1, [$r];
+ }
+ }
+ pop @ranges;
+ return \@ranges
+}
+
+use Test2::V0; plan 3 + 2;
+
+is range_list([1, 3, 4, 5, 7]), [[3, 5]], 'Example 1';
+is range_list([1, 2, 3, 6, 7, 9]), [[1, 3], [6, 7]], 'Example 2';
+is range_list([0, 1, 2, 4, 5, 6, 8, 9]), [[0, 2], [4, 6], [8, 9]], 'Example 3';
+
+is range_list([1, 3, 5, 7]), [], 'Empty';
+is range_list([1 .. 10]), [[1, 10]], 'Full';