diff options
| -rw-r--r-- | challenge-202/e-choroba/erlang/widest_valley.erl | 53 | ||||
| -rwxr-xr-x | challenge-202/e-choroba/perl/ch-1.pl | 23 | ||||
| -rwxr-xr-x | challenge-202/e-choroba/perl/ch-2.pl | 39 |
3 files changed, 115 insertions, 0 deletions
diff --git a/challenge-202/e-choroba/erlang/widest_valley.erl b/challenge-202/e-choroba/erlang/widest_valley.erl new file mode 100644 index 0000000000..5924e46287 --- /dev/null +++ b/challenge-202/e-choroba/erlang/widest_valley.erl @@ -0,0 +1,53 @@ +-module(widest_valley). +-export([widest_valley/1]). +-record(valley, {from=1, to=1, deepest=1}). + +widest_valley([]) -> + []; +widest_valley([H]) -> + [H]; +widest_valley([H,T]) -> + [H,T]; +widest_valley(L) -> + widest_valley(L, #valley{}, #valley{}, 1, 2). + +widest_valley(L, _Curr, Widest, _FlatFrom, Pos) when Pos > length(L) -> + lists:sublist(L, Widest#valley.from, + 1 + Widest#valley.to - Widest#valley.from); +widest_valley(L, Curr, Widest, FlatFrom, Pos) -> + Before = lists:nth(Pos - 1, L), + AtPos = lists:nth(Pos, L), + Deepest = lists:nth(Curr#valley.deepest, L), + Curr1 = if Before == Deepest -> + Curr#valley{deepest = if AtPos < Deepest -> + Pos; + true -> + Curr#valley.deepest + end}; + AtPos < Before -> + #valley{from=FlatFrom, deepest=Pos}; + true -> + Curr + end, + FlatFrom1 = if AtPos == Before -> + FlatFrom; + true -> + Pos + end, + Widest1 = + if Pos - Curr1#valley.from > Widest#valley.to - Widest#valley.from + -> Curr1#valley{to=Pos}; + true -> + Widest + end, + widest_valley(L, Curr1, Widest1, FlatFrom1, Pos + 1). + +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +widest_valley_test() -> + ?assert(widest_valley([1, 5, 5, 2, 8]) == [5, 5, 2, 8]), + ?assert(widest_valley([2, 6, 8, 5]) == [2, 6, 8]), + ?assert(widest_valley([9, 8, 13, 13, 2, 2, 15, 17]) == [13, 13, 2, 2, 15, 17]), + ?assert(widest_valley([2, 1, 2, 1, 3]) == [2, 1, 2]), + ?assert(widest_valley([1, 3, 3, 2, 1, 2, 3, 3, 2]) == [3, 3, 2, 1, 2, 3, 3]). +-endif. diff --git a/challenge-202/e-choroba/perl/ch-1.pl b/challenge-202/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..f1a029ccaf --- /dev/null +++ b/challenge-202/e-choroba/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +sub consecutive_odds ($array) { + my %elements; + @elements{@$array} = (); + for my $element (keys %elements) { + return 1 + if 1 == $element % 2 + && exists $elements{$element + 2} + && exists $elements{$element + 4}; + } + return 0 +} + +use Test::More tests => 4; + +is consecutive_odds([1, 5, 3, 6]), 1, 'Example 1'; +is consecutive_odds([2, 6, 3, 5]), 0, 'Example 2'; +is consecutive_odds([1, 2, 3, 4]), 0, 'Example 3'; +is consecutive_odds([2, 3, 5, 7]), 1, 'Example 4'; diff --git a/challenge-202/e-choroba/perl/ch-2.pl b/challenge-202/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..51ae8dc7b1 --- /dev/null +++ b/challenge-202/e-choroba/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +sub widest_valley($profile) { + my @widest = (1, 0); + my $from = 0; + while ($from <= $#$profile) { + my $to = my $deepest = my $flat_from = $from; + while ($to <= $#$profile + && ($deepest >= $to - 1 + || $profile->[$to] >= $profile->[ $to - 1 ]) + ) { + $deepest = $to if $profile->[$to] <= $profile->[$deepest]; + $flat_from = $to unless $profile->[$to] == $profile->[ $to - 1 ]; + @widest = ($from, $to) if $to - $from > $widest[1] - $widest[0]; + ++$to; + } + $from = $to > $#$profile ? $to : $flat_from; + } + return [@$profile[ $widest[0] .. $widest[1] ]] +} + +use Test2::V0; +plan 5 + 4; +is widest_valley([1, 5, 5, 2, 8]), [5, 5, 2, 8], 'Example 1'; +is widest_valley([2, 6, 8, 5]), [2, 6, 8], 'Exapmle 2'; +is widest_valley([9, 8, 13, 13, 2, 2, 15, 17]), + [13, 13, 2, 2, 15, 17], + 'Example 3'; +is widest_valley([1, 3, 3, 2, 1, 2, 3, 3, 2]), [3, 3, 2, 1, 2, 3, 3], + 'Example 4'; +is widest_valley([2, 1, 2, 1, 3]), [2, 1, 2], 'Example 5'; + +is widest_valley([]), [], 'Empty'; +is widest_valley([1, 1]), [1, 1], 'Flat'; +is widest_valley([1, 2]), [1, 2], 'Increasing'; +is widest_valley([2, 1]), [2, 1], 'Decreasing'; |
