diff options
| author | Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> | 2020-08-13 09:51:23 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-13 09:51:23 +0800 |
| commit | c8de82fd93764ca9967277a8f98fd9a5c549dd7a (patch) | |
| tree | 836097134363c6451a6f8d8bf65b6c3167c61c03 | |
| parent | 8b17684ca61703acd0af53c02f736fb1448545b1 (diff) | |
| download | perlweeklychallenge-club-c8de82fd93764ca9967277a8f98fd9a5c549dd7a.tar.gz perlweeklychallenge-club-c8de82fd93764ca9967277a8f98fd9a5c549dd7a.tar.bz2 perlweeklychallenge-club-c8de82fd93764ca9967277a8f98fd9a5c549dd7a.zip | |
Add files via upload
| -rw-r--r-- | ch-1.pl | 48 | ||||
| -rw-r--r-- | ch-2.pl | 52 |
2 files changed, 100 insertions, 0 deletions
diff --git a/ch-1.pl b/ch-1.pl new file mode 100644 index 0000000000..0c2c61b772 --- /dev/null +++ b/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +# Perl Weekly Challenge #072 Task 1 Sliding Windows +# task statement: +# 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. +# Usage: ch-1.pl $S $A[0] $A[1] ... $A[-1] + +use strict; +use warnings; +use List::Util qw/min/; +#use Test::More tests => 4; + +sub awindows { + my $winsize = $_[0]; + my @array = @{$_[1]}; + my @smallkids = (); + # loop: look for the smallest element on the sliding window: + for my $i (0..$#array-$winsize+1) { + push @smallkids, min @array[$i..$i+$winsize-1]; + } + return [@smallkids]; +} + +my $S; +my @A; + +if ($ARGV[0] and $ARGV[1]) { + $S = shift @ARGV; + @A = @ARGV; +} else { + $S = 3; + @A = (1, 5, 0, 2, 9, 3, 7, 6, 4, 8); +} + +print join " ", @{ awindows($S, [@A])}; +print "\n"; + + +=pod +is_deeply( awindows(3, [1, 5, 0, 2, 9, 3, 7, 6, 4, 8]), + [0, 0, 0, 2, 3, 3, 4, 4], "example provided") ; +is_deeply( awindows(3, [ 10, 47, 16, 50, 29, 21, 18, 20, 6, 30, 11]), + [10, 16, 16, 21, 18, 18, 6 , 6, 6 ], "eleven random numbers"); +is_deeply( awindows(2, [2, 3, 5, 7, 11, 13, 17]), + [2, 3, 5, 7, 11, 13], "ascending sequences"); +is_deeply( awindows(4, [reverse 1..10]), + [reverse 1..7], "descending first 10 positive integers"); +=cut diff --git a/ch-2.pl b/ch-2.pl new file mode 100644 index 0000000000..bc69ee8ba7 --- /dev/null +++ b/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl +# Perl Weekly Challenge #072 Task 2 Smallest Neighbour +# task statement: +# You are given an array of integers @A. +# Write a script to create an array that +# represents the smallest element to the +# left of each corresponding index. +# If none found then use 0. +# Usage: ch-2.pl @A + +use strict; +use warnings; +#use Test::More tests => 5; + +my @A; + +if (@ARGV) {@A = @ARGV;} else {@A = (7,8,3,12,10);} + +sub leastneigh { + my @array = @{$_[0]}; + my $youngest = $array[0]; + my @smallkids = (); + push @smallkids, 0; + for my $num (@array[1..$#array]) { + if ($num < $youngest) { + push @smallkids, 0; + $youngest = $num; + } + else { + push @smallkids, $youngest; + } + + } + return [@smallkids]; + #return [0, 7, 0, 3, 3]; +} + +print join " ", @{ leastneigh([@A]) }; +print "\n"; + +=pod +is_deeply( leastneigh([7, 8, 3, 12, 10]), + [0, 7, 0, 3, 3], "example1 provided") ; +is_deeply( leastneigh([4, 6, 5]), + [0, 4, 4], "example2 provided") ; +is_deeply( leastneigh([ 10, 47, 16, 50, 29, 21, 18, 20, 6, 30, 11]), + [0, 10, 10, 10, 10, 10, 10, 10, 0, 6, 6 ], "eleven random numbers"); +is_deeply( leastneigh([2, 3, 5, 7, 11, 13, 17]), + [0, 2, 2, 2, 2, 2, 2], "ascending sequences"); +is_deeply( leastneigh([reverse (1..4)]), + [ 0 ,0,0,0], "descending first 4 positive integers"); +=cut |
