diff options
| author | E. Choroba <choroba@matfyz.cz> | 2022-12-21 21:47:10 +0100 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2022-12-21 21:47:10 +0100 |
| commit | 0477feded547512347ea95c19a7f0d562e9a3394 (patch) | |
| tree | 5da3728aac05924f2dda812d8c3615329b5a99b4 | |
| parent | d05040719c41d928cb4663bac89448f14fd74268 (diff) | |
| download | perlweeklychallenge-club-0477feded547512347ea95c19a7f0d562e9a3394.tar.gz perlweeklychallenge-club-0477feded547512347ea95c19a7f0d562e9a3394.tar.bz2 perlweeklychallenge-club-0477feded547512347ea95c19a7f0d562e9a3394.zip | |
Add solution to 196: Pattern 132 & Range List by E. Choroba
| -rwxr-xr-x | challenge-196/e-choroba/perl/ch-1.pl | 27 | ||||
| -rwxr-xr-x | challenge-196/e-choroba/perl/ch-2.pl | 27 |
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'; |
