diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-05 00:25:19 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-05 00:25:19 +0000 |
| commit | adde47cbb7cb48dea0703a63bad699fed751a47e (patch) | |
| tree | a510eaf736c1cd3df27d1fd32c446b2e23e3a2cb | |
| parent | ed504b74847f0f3e642014e5d4adca87686f0703 (diff) | |
| parent | f1ae22a8e490c4805c14b90a97bee9bafc96333d (diff) | |
| download | perlweeklychallenge-club-adde47cbb7cb48dea0703a63bad699fed751a47e.tar.gz perlweeklychallenge-club-adde47cbb7cb48dea0703a63bad699fed751a47e.tar.bz2 perlweeklychallenge-club-adde47cbb7cb48dea0703a63bad699fed751a47e.zip | |
Merge pull request #7517 from carlos157oliveira/challenge-202
solution to challenge 202
| -rw-r--r-- | challenge-202/carlos-oliveira/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-202/carlos-oliveira/perl/ch-2.pl | 45 |
2 files changed, 67 insertions, 0 deletions
diff --git a/challenge-202/carlos-oliveira/perl/ch-1.pl b/challenge-202/carlos-oliveira/perl/ch-1.pl new file mode 100644 index 0000000000..6094ad697e --- /dev/null +++ b/challenge-202/carlos-oliveira/perl/ch-1.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +use v5.36; + +use List::MoreUtils qw(slideatatime all); +use Const::Fast; + +sub has_three_consecutive_odds { + const my $step_size => 1; + const my $window_size => 3; + my $it = slideatatime $step_size, $window_size, @_; + my @vals; + while (@vals = $it->() and @vals == $window_size) { + return 1 if all { $_ % 2 } @vals; + } + return 0; +} + +say has_three_consecutive_odds 1, 5, 3, 6; +say has_three_consecutive_odds 2, 6, 3, 5; +say has_three_consecutive_odds 1, 2, 3, 4; +say has_three_consecutive_odds 2, 3, 5, 7; diff --git a/challenge-202/carlos-oliveira/perl/ch-2.pl b/challenge-202/carlos-oliveira/perl/ch-2.pl new file mode 100644 index 0000000000..75ac9a291e --- /dev/null +++ b/challenge-202/carlos-oliveira/perl/ch-2.pl @@ -0,0 +1,45 @@ +use strict; +use warnings; +use v5.36; + +use Const::Fast; +use List::MoreUtils qw(slideatatime firstidx slide all); + +sub pick_widest_valley { + return @_ if @_ < 3; + const my $step_size => 1; + for my $window_size (reverse 3 .. @_) { + my $it = slideatatime $step_size, $window_size, @_; + my @vals; + while (@vals = $it->() and @vals == $window_size) { + # where the non-increasing part necessarily stops? we'll start after it + my $abyss = firstidx { $_ } slide { $a < $b } @vals; + $abyss++; + return @vals + # firstidx returns -1 when an element is not found, 0 after our increment: + # this slice only has the non-increasing part and either part can be empty + if $abyss == 0 + # the $abyss can't be the last element because the slide function needs at least two elements + || $abyss == $#vals + # everything after the $abyss must be non-decreasing + || all { $_ } slide { $a <= $b } @vals[$abyss..$#vals]; + } + } + # any two elements shall suffice if the windows above didn't solve + return @_[0,1]; +} + +say join ", ", pick_widest_valley 1, 5, 5, 2, 8; +say join ", ", pick_widest_valley 2, 6, 8, 5; +say join ", ", pick_widest_valley 9, 8, 13, 13, 2, 2, 15, 17; +say join ", ", pick_widest_valley 2, 1, 2, 1, 3; +say join ", ", pick_widest_valley 1, 3, 3, 2, 1, 2, 3, 3, 2; +say join ", ", pick_widest_valley 5, 8, 6, 2; + +# see results for short lists +say join ", ", pick_widest_valley 1, 3; +say join ", ", pick_widest_valley 3, 1; +say join ", ", pick_widest_valley 3, 3; +say join ", ", pick_widest_valley 5; +say join ", ", pick_widest_valley 1, 5, 1; +say pick_widest_valley == 0 ? 'empty' : 'not empty'; |
