From fb278b5ca2deb3f4a6081786736a68e90e14bd0d Mon Sep 17 00:00:00 2001 From: wanderdoc Date: Mon, 10 Aug 2020 19:55:03 +0200 Subject: Solutions to challenge-073. --- challenge-073/wanderdoc/R/ch-1.R | 6 +++++ challenge-073/wanderdoc/perl/ch-1.pl | 47 ++++++++++++++++++++++++++++++++++++ challenge-073/wanderdoc/perl/ch-2.pl | 37 ++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 challenge-073/wanderdoc/R/ch-1.R create mode 100644 challenge-073/wanderdoc/perl/ch-1.pl create mode 100644 challenge-073/wanderdoc/perl/ch-2.pl diff --git a/challenge-073/wanderdoc/R/ch-1.R b/challenge-073/wanderdoc/R/ch-1.R new file mode 100644 index 0000000000..57c113c546 --- /dev/null +++ b/challenge-073/wanderdoc/R/ch-1.R @@ -0,0 +1,6 @@ +library(runner) +size <- 3 +minis <- runner(c(1, 5, 0, 2, 9, 3, 7, 6, 4, 8), k = size, f = min) + +(minis <- minis[size:length(minis)]) # 0 0 0 2 3 3 4 4 +rm(size, minis) diff --git a/challenge-073/wanderdoc/perl/ch-1.pl b/challenge-073/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..2e2686e99e --- /dev/null +++ b/challenge-073/wanderdoc/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt + +You are given an array of integers @A and sliding window size $S. +Write a script to create an array of min from each sliding window. + +Example + +Input: @A = (1, 5, 0, 2, 9, 3, 7, 6, 4, 8) and $S = 3 + +Output: (0, 0, 0, 2, 3, 3, 4, 4) +[(1 5 0) 2 9 3 7 6 4 8] = Min (0) +[1 (5 0 2) 9 3 7 6 4 8] = Min (0) +[1 5 (0 2 9) 3 7 6 4 8] = Min (0) +[1 5 0 (2 9 3) 7 6 4 8] = Min (2) +[1 5 0 2 (9 3 7) 6 4 8] = Min (3) +[1 5 0 2 9 (3 7 6) 4 8] = Min (3) +[1 5 0 2 9 3 (7 6 4) 8] = Min (4) +[1 5 0 2 9 3 7 (6 4 8)] = Min (4) +=cut + + + +use List::Util qw(min); +use Test::More; + +sub sliding_min +{ + my ($aref, $s) = @_; + my @min; + for my $i ( 0 .. $#$aref - $s + 1 ) + { + push @min, min(@{$aref}[$i .. $i+$s-1]); + } + return @min; + + +} + +is_deeply([sliding_min([1, 5, 0, 2, 9, 3, 7, 6, 4, 8], 3)], [0, 0, 0, 2, 3, 3, 4, 4], 'Example 1'); + +is_deeply([sliding_min([1, 5, 0, 2, 9, 3, 7, 6, 4, 8], 5)], [0, 0, 0, 2, 3, 3], 'Example 2'); + +done_testing(); \ No newline at end of file diff --git a/challenge-073/wanderdoc/perl/ch-2.pl b/challenge-073/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..028daa43db --- /dev/null +++ b/challenge-073/wanderdoc/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given an array of integers @A. +Write a script to create an array that represents the smaller element to the left of each corresponding index. If none found then use 0. +Example 1 Input: @A = (7, 8, 3, 12, 10) Output: (0, 7, 0, 3, 3) +Example 2 Input: @A = (4, 6, 5) Output: (0, 4, 4) +=cut + + + + + + +use List::Util qw(min); +use Test::More; + + +sub smallest_neighbour +{ + my @arr = @_; + my @small; + $small[0] = 0; + + for my $i ( 1 .. $#arr ) + { + push @small, min(@arr[0..$i-1]) < $arr[$i] ? min(@arr[0..$i-1]) : 0; + } + return @small; +} + + +is_deeply([smallest_neighbour(7,8,3,12,10)], [0,7,0,3,3], 'Example 1'); +is_deeply([smallest_neighbour(4,6,5)], [0,4,4], 'Example 2'); +done_testing(); \ No newline at end of file -- cgit