From 16e2466518b57c9a3cc1f62a90474fa251e3c7c4 Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Thu, 13 Aug 2020 09:53:54 +0800 Subject: Update and rename ch-2.pl to challenge-073/cheok-yin-fung/perl/ch-2.pl --- ch-2.pl | 52 ------------------------------- challenge-073/cheok-yin-fung/perl/ch-2.pl | 52 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 52 deletions(-) delete mode 100644 ch-2.pl create mode 100644 challenge-073/cheok-yin-fung/perl/ch-2.pl diff --git a/ch-2.pl b/ch-2.pl deleted file mode 100644 index bc69ee8ba7..0000000000 --- a/ch-2.pl +++ /dev/null @@ -1,52 +0,0 @@ -#!/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 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..cf85c183dc --- /dev/null +++ b/challenge-073/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/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 + +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 -- cgit