diff options
| -rw-r--r-- | challenge-073/e-choroba/erlang/pwc073.erl | 24 | ||||
| -rwxr-xr-x | challenge-073/e-choroba/perl5/ch-1.pl | 17 | ||||
| -rwxr-xr-x | challenge-073/e-choroba/perl5/ch-2.pl | 23 |
3 files changed, 64 insertions, 0 deletions
diff --git a/challenge-073/e-choroba/erlang/pwc073.erl b/challenge-073/e-choroba/erlang/pwc073.erl new file mode 100644 index 0000000000..fa45be6d47 --- /dev/null +++ b/challenge-073/e-choroba/erlang/pwc073.erl @@ -0,0 +1,24 @@ +-module(pwc073). +-export([min_sliding_window/2, smallest_neighbour/1]). + +min_sliding_window(List, WinSize) when length(List) < WinSize -> + []; +min_sliding_window(List, WinSize) -> + [lists:min(lists:sublist(List, WinSize)) + | min_sliding_window(lists:nthtail(1, List), WinSize)]. + +smallest_neighbour([H|T]) -> + [0 | smallest_neighbour(T, H)]. + +smallest_neighbour([], _Min) -> + []; +smallest_neighbour([H|T], Min) -> + [ case Min < H of + true -> Min; + false -> 0 + end + | smallest_neighbour(T, case H < Min of + true -> H; + false -> Min + end ) + ]. diff --git a/challenge-073/e-choroba/perl5/ch-1.pl b/challenge-073/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..f91a18417c --- /dev/null +++ b/challenge-073/e-choroba/perl5/ch-1.pl @@ -0,0 +1,17 @@ +#! /usr/bin/perl +use warnings; +use strict; + +use List::Util qw{ min }; + +sub min_sliding_window { + my ($arr, $size) = @_; + [ map min(@$arr[$_ .. $_ + $size - 1]), + 0 .. $#$arr - $size + 1 ] +} + +use Test::More tests => 1; +is_deeply + min_sliding_window([1, 5, 0, 2, 9, 3, 7, 6, 4, 8], 3), + [0, 0, 0, 2, 3, 3, 4, 4]; + diff --git a/challenge-073/e-choroba/perl5/ch-2.pl b/challenge-073/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..17ccef9fe3 --- /dev/null +++ b/challenge-073/e-choroba/perl5/ch-2.pl @@ -0,0 +1,23 @@ +#! /usr/bin/perl +use warnings; +use strict; + +sub smallest_neighbour { + my ($arr) = @_; + my @neighbours = 0; + my $min = $arr->[0]; + for my $i (1 .. $#$arr) { + # It seems the minimum must also be less than the current element. + push @neighbours, $min < $arr->[$i] ? $min : 0; + $min = $arr->[$i] if $arr->[$i] < $min; + } + return \@neighbours +} + +use Test::More tests => 2; + +is_deeply smallest_neighbour([7, 8, 3, 12, 10]), + [0, 7, 0, 3, 3]; + +is_deeply smallest_neighbour([4, 6, 5]), + [0, 4, 4]; |
