diff options
| -rw-r--r-- | challenge-071/cheok-yin-fung/perl/ch-1.pl | 17 |
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; |
