diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-10 22:19:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-10 22:19:01 +0100 |
| commit | 88b691f13dc724c23f64c11ed71d6e433c000cf9 (patch) | |
| tree | 678f445ab2fea265ad08324d8eb46ff4630bf2bc | |
| parent | bf2672d4d1304edb92e7118fa46227fa3ba972e3 (diff) | |
| parent | 4dd31344270f521ed88bd72806846c879b9c9a9a (diff) | |
| download | perlweeklychallenge-club-88b691f13dc724c23f64c11ed71d6e433c000cf9.tar.gz perlweeklychallenge-club-88b691f13dc724c23f64c11ed71d6e433c000cf9.tar.bz2 perlweeklychallenge-club-88b691f13dc724c23f64c11ed71d6e433c000cf9.zip | |
Merge pull request #10819 from pme/challenge-202
challenge-202
| -rwxr-xr-x | challenge-202/peter-meszaros/perl/ch-1.pl | 63 | ||||
| -rwxr-xr-x | challenge-202/peter-meszaros/perl/ch-2.pl | 87 |
2 files changed, 150 insertions, 0 deletions
diff --git a/challenge-202/peter-meszaros/perl/ch-1.pl b/challenge-202/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..fbbd939132 --- /dev/null +++ b/challenge-202/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Consecutive Odds + +Submitted by: Mohammad S Anwar + +You are given an array of integers. + +Write a script to print 1 if there are THREE consecutive odds in the given +array otherwise print 0. + +=head2 Example 1 + + Input: @array = (1,5,3,6) + Output: 1 + +=head2 Example 2 + + Input: @array = (2,6,3,5) + Output: 0 + +=head2 Example 3 + + Input: @array = (1,2,3,4) + Output: 0 + +=head2 Example 4 + + Input: @array = (2,3,5,7) + Output: 1 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[1, 5, 3, 6], 1, 'Example 1'], + [[2, 6, 3, 5], 0, 'Example 2'], + [[1, 2, 3, 4], 0, 'Example 3'], + [[2, 3, 5, 7], 1, 'Example 4'], +]; + +sub consecutive_odds +{ + my $l = shift; + + for my $i (0 .. $#$l-2) { + return 1 if $l->[$i] % 2 && + $l->[$i+1] % 2 && + $l->[$i+2] % 2; + } + return 0; +} + +for (@$cases) { + is(consecutive_odds($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-202/peter-meszaros/perl/ch-2.pl b/challenge-202/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..bc42e1129f --- /dev/null +++ b/challenge-202/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,87 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Widest Valley + +Submitted by: E. Choroba + +Given a profile as a list of altitudes, return the leftmost widest valley. A +valley is defined as a subarray of the profile consisting of two parts: the +first part is non-increasing and the second part is non-decreasing. Either part +can be empty. + +=head2 Example 1 + + Input: 1, 5, 5, 2, 8 + Output: 5, 5, 2, 8 + +=head2 Example 2 + + Input: 2, 6, 8, 5 + Output: 2, 6, 8 + +=head2 Example 3 + + Input: 9, 8, 13, 13, 2, 2, 15, 17 + Output: 13, 13, 2, 2, 15, 17 + +=head2 Example 4 + + Input: 2, 1, 2, 1, 3 + Output: 2, 1, 2 + +=head2 Example 5 + + Input: 1, 3, 3, 2, 1, 2, 3, 3, 2 + Output: 3, 3, 2, 1, 2, 3, 3 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[1, 5, 5, 2, 8], [ 5, 5, 2, 8], 'Example 1'], + [[2, 6, 8, 5], [ 2, 6, 8], 'Example 2'], + [[9, 8, 13, 13, 2, 2, 15, 17], [13, 13, 2, 2, 15, 17], 'Example 3'], + [[2, 1, 2, 1, 3], [ 2, 1, 2], 'Example 4'], + [[1, 3, 3, 2, 1, 2, 3, 3, 2], [ 3, 3, 2, 1, 2, 3, 3], 'Example 5'], +]; + +sub get_valley +{ + my $lst = shift; + my $curr = shift; + + while ($curr < $#$lst) { + last if $lst->[$curr+1] > $lst->[$curr]; + ++$curr; + } + while ($curr < $#$lst) { + last if $lst->[$curr+1] < $lst->[$curr]; + ++$curr; + } + return $curr; +} + +sub widest_valley +{ + my $l = shift; + + my @valley; + for my $start (0 .. $#$l-1) { + my $end = get_valley($l, $start); + @valley = $l->@[$start .. $end] if ($end - $start + 1) > @valley; + } + + return \@valley; +} + +for (@$cases) { + is(widest_valley($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; + |
