aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-10 21:26:45 +0100
committerGitHub <noreply@github.com>2020-08-10 21:26:45 +0100
commit8b436bbbe96c4fc03658c7f097aff7e983c92ce6 (patch)
treec0a5169f41f5008440cff16881fe195d2678106c
parent9505faaf0fdd834b3598f85da72dbd9bc6e80c23 (diff)
parent9480ab1861f74eab774360367afa0bde11f96287 (diff)
downloadperlweeklychallenge-club-8b436bbbe96c4fc03658c7f097aff7e983c92ce6.tar.gz
perlweeklychallenge-club-8b436bbbe96c4fc03658c7f097aff7e983c92ce6.tar.bz2
perlweeklychallenge-club-8b436bbbe96c4fc03658c7f097aff7e983c92ce6.zip
Merge pull request #2068 from choroba/ech073
Add solutions to 073 by E. Choroba in Perl and Erlang
-rw-r--r--challenge-073/e-choroba/erlang/pwc073.erl24
-rwxr-xr-xchallenge-073/e-choroba/perl5/ch-1.pl17
-rwxr-xr-xchallenge-073/e-choroba/perl5/ch-2.pl23
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];