diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2023-01-31 11:57:55 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2023-01-31 11:57:55 -0500 |
| commit | 45a9bd05635c9b4446027ae1cc2927fd1e1831d8 (patch) | |
| tree | 85a670d04cdcfd0b9214ca9b676322ee07158657 /challenge-202 | |
| parent | 81e796219fe7ddaf5f50e72a0702297a2dee6db7 (diff) | |
| download | perlweeklychallenge-club-45a9bd05635c9b4446027ae1cc2927fd1e1831d8.tar.gz perlweeklychallenge-club-45a9bd05635c9b4446027ae1cc2927fd1e1831d8.tar.bz2 perlweeklychallenge-club-45a9bd05635c9b4446027ae1cc2927fd1e1831d8.zip | |
#202 DAJ
Diffstat (limited to 'challenge-202')
| -rw-r--r-- | challenge-202/dave-jacoby/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-202/dave-jacoby/perl/ch-2.pl | 57 |
2 files changed, 97 insertions, 0 deletions
diff --git a/challenge-202/dave-jacoby/perl/ch-1.pl b/challenge-202/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..d64974b1cd --- /dev/null +++ b/challenge-202/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + [ 1, 5, 3, 6 ], + [ 2, 6, 3, 5 ], + [ 1, 2, 3, 4 ], + [ 2, 3, 5, 7 ], + [ 2, 3, 4, 7, 9, 11, 13 ], + +); + +for my $e (@examples) { + my $list = join ',', $e->@*; + my $out = consecutive_odds( $e->@* ); + say <<"END"; + Input: \@array = ($list) + Output: $out +END +} + +sub consecutive_odds ( @array ) { + my $max = -3 + scalar @array; +OUTER: for my $i ( 0 .. $max ) { + for my $j ( 0 .. 2 ) { + my $n = $array[ $i + $j ]; + next OUTER if !is_odd($n); + return 1 if $j == 2; + } + } + return 0; +} + +sub is_odd ( $n ) { + return $n % 2 ? 1 : 0; +} diff --git a/challenge-202/dave-jacoby/perl/ch-2.pl b/challenge-202/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..cbe4eebfa7 --- /dev/null +++ b/challenge-202/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ sum0 uniq }; + +my @examples = ( + + [ 1, 5, 5, 2, 8 ], + [ 2, 6, 8, 5 ], + [ 9, 8, 13, 13, 2, 2, 15, 17 ], + [ 2, 1, 2, 1, 3 ], + [ 1, 3, 3, 2, 1, 2, 3, 3, 2 ], +); + +for my $e (@examples) { + my $example = join ', ', $e->@*; + my @valley = widest_valley( $e->@* ); + my $valley = join ', ', @valley; + say <<"END"; + Input: \$n = $example + Output: $valley +END +} + +sub widest_valley ( @array ) { + my @output; + my $end = -1 + scalar @array; + +OUTER: for my $i ( 0 .. $end ) { + + # 0 = non-increasing, 1 = non-decreasing + my $state = 0; + my @local; + for my $j ( $i .. $end ) { + my $n = $array[$j]; + + # descending + if ( $state == 0 && scalar @local && $n > $local[-1] ) { + $state = 1; + } + elsif ( $state == 1 && $n < $local[-1] ) { + next OUTER; + } + push @local, $n; + my @copy = @local; + push @output, \@copy if scalar @copy > 2; + } + } + + @output = sort { scalar $b->@* <=> scalar $a->@* } @output; + return () unless scalar @output; + return $output[0]->@*; +} + |
