aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-16 17:06:27 +0100
committerGitHub <noreply@github.com>2020-08-16 17:06:27 +0100
commit6a348cbdec1a1c243062883a7b61cd439887e2ba (patch)
tree7e0f289690f623023a5358c14a199384909c5dc6
parent172613df56257175f0ff16ace49e68ed7c0c956f (diff)
parent00576254dae94b522130208c0ac3e19167827ddd (diff)
downloadperlweeklychallenge-club-6a348cbdec1a1c243062883a7b61cd439887e2ba.tar.gz
perlweeklychallenge-club-6a348cbdec1a1c243062883a7b61cd439887e2ba.tar.bz2
perlweeklychallenge-club-6a348cbdec1a1c243062883a7b61cd439887e2ba.zip
Merge pull request #2084 from E7-87-83/master
Cheok Yin's submission for PWC #073
-rw-r--r--challenge-073/cheok-yin-fung/BLOG.txt1
-rw-r--r--challenge-073/cheok-yin-fung/common-lisp/ch-1.lsp35
-rw-r--r--challenge-073/cheok-yin-fung/perl/ch-1.pl48
-rw-r--r--challenge-073/cheok-yin-fung/perl/ch-1a.pl94
-rw-r--r--challenge-073/cheok-yin-fung/perl/ch-2.pl59
5 files changed, 237 insertions, 0 deletions
diff --git a/challenge-073/cheok-yin-fung/BLOG.txt b/challenge-073/cheok-yin-fung/BLOG.txt
new file mode 100644
index 0000000000..c0bd092850
--- /dev/null
+++ b/challenge-073/cheok-yin-fung/BLOG.txt
@@ -0,0 +1 @@
+http://blogs.perl.org/users/c_y_fung/2020/08/cys-recent-submission-for-pwc068-073.html
diff --git a/challenge-073/cheok-yin-fung/common-lisp/ch-1.lsp b/challenge-073/cheok-yin-fung/common-lisp/ch-1.lsp
new file mode 100644
index 0000000000..f6d014a6f3
--- /dev/null
+++ b/challenge-073/cheok-yin-fung/common-lisp/ch-1.lsp
@@ -0,0 +1,35 @@
+; 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.
+
+( defun smallest (a)
+ (if (> (length a) 2)
+ (min (first a) (smallest (rest a)))
+ (min (first a) (cadr a))
+ )
+)
+
+(setf INPUT (list 1 5 0 2 9 3 7 6 4 8)
+ B INPUT)
+
+(setf S 3
+ ANS ()
+ window ()
+)
+
+
+; initialization of the variable window
+(setf window (list (first INPUT)))
+(loop for i from 1 to (- S 1)
+ do (setf window (append window (list (nth i INPUT))))
+)
+
+(loop for i from 0 to (- (length INPUT) S ) do
+ (setf ANS (append ANS (list (smallest window)) )
+ B (rest B)
+ window (append (rest window) (list (nth (- S 1) B)))
+ )
+)
+
+ANS
diff --git a/challenge-073/cheok-yin-fung/perl/ch-1.pl b/challenge-073/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..61603e83a9
--- /dev/null
+++ b/challenge-073/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+# Perl Weekly Challenge #073 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/challenge-073/cheok-yin-fung/perl/ch-1a.pl b/challenge-073/cheok-yin-fung/perl/ch-1a.pl
new file mode 100644
index 0000000000..967cf76687
--- /dev/null
+++ b/challenge-073/cheok-yin-fung/perl/ch-1a.pl
@@ -0,0 +1,94 @@
+#!/usr/bin/perl
+# Perl Weekly Challenge #073 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-1a.pl $S $A[0] $A[1] ... $A[-1]
+# for large array size and large windows size ,
+# while $S is much smaller than size of the array though
+
+use strict;
+use warnings;
+#use Test::More tests => 7;
+
+my $S;
+my @A;
+
+sub choosemin {
+ my @array = @_;
+ my $min = $array[0];
+ my $minposition = 0;
+ for my $i (1..$#array) {
+ if ($array[$i] <= $min) {
+ $min = $array[$i];
+ $minposition = $i;
+ }
+ }
+ return [$min, $minposition];
+}
+
+sub awindows {
+ my $winsize = $_[0];
+ my @array = @{$_[1]};
+ my @smallkids = ();
+ my $min = undef;
+ my $minposition = undef;
+
+ for my $winposition (0..$#array-$winsize+1) {
+ if ($min) {
+ my $i = $winsize - 1;
+ my $ele = $array[$winposition+$i];
+ if ($ele <= $min) {
+ $minposition = $i;
+ $min = $ele;
+ }
+ } else {
+ ($min, $minposition)
+ = @{choosemin(@array[$winposition..$winposition+$winsize-1])};
+ }
+ push @smallkids, $min;
+ if ($minposition == 0) {
+ $min = undef;
+ $minposition = undef;
+ } else {
+ $minposition--;
+ }
+ }
+ return [@smallkids];
+}
+
+if ($ARGV[0] and $ARGV[1]) {
+ $S = shift @ARGV;
+ @A = @ARGV;
+} else {
+ $S = 7;
+ @A = qw/15 23 31 39 9 22 31 25 50 46 44 19 15 9 41 45 8 35 20 10/ ;
+}
+
+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");
+is_deeply( awindows(2, [qw/7 3 19 17 1 3 37 45 4 23 39 18 17 9 14
+ 32 25 34 22 21 45 37 46 4 31 15 41 23 8 20/]),
+ [qw/3 3 17 1 1 3 37 4 4 23 18 17 9 9 14 25 25
+ 22 21 21 37 37 4 4 15 15 23 8 8/],
+ "oh a long list, window size is 3");
+is_deeply( awindows(3, [qw/7 3 19 17 1 3 37 45 4 23 39 18 17 9 14
+ 32 25 34 22 21 45 37 46 4 31 15 41 23 8 20/]),
+ [qw/3 3 1 1 1 3 4 4 4 18 17 9 9 9 14 25 22 21 21 21 37
+ 4 4 4 15 15 8 8/], "a list same as above but window size is 3");
+is_deeply( awindows(7, [ qw/15 23 31 39 9 22 31 25 50 46 44
+ 19 15 9 41 45 8 35 20 10/ ]),
+ [qw/9 9 9 9 9 19 15 9 9 9 8 8 8 8/],
+ "a long list with 20 elements, with window size 7");
+=cut
diff --git a/challenge-073/cheok-yin-fung/perl/ch-2.pl b/challenge-073/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..e8b9031376
--- /dev/null
+++ b/challenge-073/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+# Perl Weekly Challenge #073 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
+# for input: (2, 2, 2, 3, 2, 4), should it be (0, 0, 0, 2, 0, 2) or (0, 2, 2, 2, 2, 2)...?
+# I choose the former here.
+
+# Or, the task statement should be clarified as:
+# ... smallest element to the left of ..., while that element is smaller than the indexed number
+# (verbose...)
+
+use strict;
+use warnings;
+#use Test::More tests => 6;
+
+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;
+ }
+ elsif ($num > $youngest) {
+ push @smallkids, $youngest;
+ } else { # $num == $youngest
+ push @smallkids, 0;
+ }
+ }
+ return [@smallkids];
+}
+
+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