aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFung Cheok Yin <61836418+E7-87-83@users.noreply.github.com>2020-08-13 08:48:54 +0800
committerGitHub <noreply@github.com>2020-08-13 08:48:54 +0800
commit8b17684ca61703acd0af53c02f736fb1448545b1 (patch)
treecf8c456eb7aca29922a4abb5dc7e9cde74e7ab1c
parentcd819ee85e2758e1a02e08ba45f8c8de08a85a5d (diff)
downloadperlweeklychallenge-club-8b17684ca61703acd0af53c02f736fb1448545b1.tar.gz
perlweeklychallenge-club-8b17684ca61703acd0af53c02f736fb1448545b1.tar.bz2
perlweeklychallenge-club-8b17684ca61703acd0af53c02f736fb1448545b1.zip
Fix
-rw-r--r--challenge-071/cheok-yin-fung/perl/ch-1.pl17
1 files changed, 12 insertions, 5 deletions
diff --git a/challenge-071/cheok-yin-fung/perl/ch-1.pl b/challenge-071/cheok-yin-fung/perl/ch-1.pl
index 996474cd7a..15a144b485 100644
--- a/challenge-071/cheok-yin-fung/perl/ch-1.pl
+++ b/challenge-071/cheok-yin-fung/perl/ch-1.pl
@@ -10,29 +10,36 @@ use warnings;
my $N;
my @seq = ();
my @peak_eles = ();
+my %unique = (); #edited on 13th Aug
if ($ARGV[0]) {$N = $ARGV[0];} else {$N = 10;}
-while ($#seq < $N) {
+
+
+while ($#seq < $N-1) { #edited on 13th Aug
my $a = 1+int(rand(50));
- push @seq, $a unless exists $unique{$a} ;
+ push @seq, $a unless exists $unique{$a} ;
$unique{ $a } = 1;
# notes on 10min before deadline: I forgot to implement the uniqueness
# requirement until I saw laurent_r's blog.
# Therefore, The code of generating the close to his.
}
-sub checkpeak {
+sub checkpeak_inside {
my $p = $_[0];
if ($seq[$p-1] < $seq[$p] and $seq[$p+1] < $seq[$p]) {
push @peak_eles, $seq[$p];
}
}
-for (1..$N-2) {
- checkpeak($_);
+if ($seq[0]>$seq[1]) {push @peak_eles, $seq[0];} #edited on 13th Aug
+
+for (1..$#seq-1) {
+ checkpeak_inside($_);
}
+if ($seq[-1]>$seq[-2]) {push @peak_eles, $seq[-1];} #edited on 13th Aug
+
print "Array: [ ", join ", ", @seq;
print "]\n";
print "Peak: [ ", join ", ", @peak_eles;