aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFung Cheok Yin <61836418+E7-87-83@users.noreply.github.com>2020-08-15 09:28:04 +0800
committerGitHub <noreply@github.com>2020-08-15 09:28:04 +0800
commit0890a965e295bec398890f83a07ccb4f05841993 (patch)
tree56c47a462f006425e0c25ec08ea93a0ed85fb7e6
parent5986f42cc32c78306bbbc7ab44f517d3089208cb (diff)
downloadperlweeklychallenge-club-0890a965e295bec398890f83a07ccb4f05841993.tar.gz
perlweeklychallenge-club-0890a965e295bec398890f83a07ccb4f05841993.tar.bz2
perlweeklychallenge-club-0890a965e295bec398890f83a07ccb4f05841993.zip
Update ch-2.pl
For input: (2, 2, 2, 3, 2, 4), I choose (0, 0, 0, 2, 0, 2) instead of (0, 2, 2, 2, 2, 2).
-rw-r--r--challenge-073/cheok-yin-fung/perl/ch-2.pl13
1 files changed, 9 insertions, 4 deletions
diff --git a/challenge-073/cheok-yin-fung/perl/ch-2.pl b/challenge-073/cheok-yin-fung/perl/ch-2.pl
index d814efcaf0..071e9f4dea 100644
--- a/challenge-073/cheok-yin-fung/perl/ch-2.pl
+++ b/challenge-073/cheok-yin-fung/perl/ch-2.pl
@@ -8,12 +8,15 @@
# 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)...?
-# Or, the task statement should be modified as: ... smallest and smaller element to the left of ...
+# 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 => 5;
+#use Test::More tests => 6;
my @A;
@@ -29,10 +32,11 @@ sub leastneigh {
push @smallkids, 0;
$youngest = $num;
}
- else {
+ elsif ($num > $youngest) { # $num > $youngest
push @smallkids, $youngest;
+ } else { # $num == $youngest
+ push @smallkids, 0;
}
-
}
return [@smallkids];
}
@@ -40,6 +44,7 @@ sub leastneigh {
print join " ", @{ leastneigh([@A]) };
print "\n";
+
=pod
is_deeply( leastneigh([7, 8, 3, 12, 10]),
[0, 7, 0, 3, 3], "example1 provided") ;