aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-03 05:06:39 +0100
committerGitHub <noreply@github.com>2020-10-03 05:06:39 +0100
commitab6f0f78dd2ed59ef09369f8de4152cbeea3c786 (patch)
tree7ed8ff235ac503af4531b54afcc5cc6593778848
parent029fb0c911912784279f1bcfc14f32eb1dbff466 (diff)
parent5ad7037145075f5b17b00355ad7fc9abe0d50da4 (diff)
downloadperlweeklychallenge-club-ab6f0f78dd2ed59ef09369f8de4152cbeea3c786.tar.gz
perlweeklychallenge-club-ab6f0f78dd2ed59ef09369f8de4152cbeea3c786.tar.bz2
perlweeklychallenge-club-ab6f0f78dd2ed59ef09369f8de4152cbeea3c786.zip
Merge pull request #2432 from E7-87-83/master
CY's submission for Challenge #080
-rw-r--r--challenge-080/cheok-yin-fung/perl/ch-1.pl34
-rw-r--r--challenge-080/cheok-yin-fung/perl/ch-2.pl59
2 files changed, 93 insertions, 0 deletions
diff --git a/challenge-080/cheok-yin-fung/perl/ch-1.pl b/challenge-080/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..54f8cf7f67
--- /dev/null
+++ b/challenge-080/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+# The Weekly Challenge - Perl & Raku #080 Task 1
+use strict;
+use warnings;
+#use Test::More tests => 5;
+use feature qw/say/;
+
+my @N;
+if ($ARGV[0]) {@N = @ARGV;} else {@N = (1, 0, -1);}
+
+sub smallest {
+ my @arr = @_;
+ my $lenarr = scalar @arr;
+ my %space = ( $_ => undef ) for 1..(1 + $lenarr);
+
+ foreach (@arr) {
+ $space{$_} = 1 if $_ >= 1 and $_ <= $lenarr+1 ;
+ }
+ for (1..$lenarr+1) {
+ if (!$space{$_}) {
+ return $_;
+ }
+ }
+}
+
+say smallest(@N);
+
+=pod
+ok smallest(1, 2, 3) == 4, "pass test case 1 2 3";
+ok smallest(5, 2, -2, 0) == 1, "pass test case 2";
+ok smallest(1, 8, -1) == 2, "pass test case 3";
+ok smallest(2, 0, -1) == 1, "pass test case 4";
+ok smallest(1, 6, 7, 28, -5, 0, 2, 3) == 4, "pass test case 5";
+=cut
diff --git a/challenge-080/cheok-yin-fung/perl/ch-2.pl b/challenge-080/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..8f946427af
--- /dev/null
+++ b/challenge-080/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+# The Weekly Challenge #080 Task 2 Count Candies
+use strict;
+use warnings;
+#use Test::More tests => 7;
+use List::Util qw/sum/;
+use feature qw/say/;
+
+my @N;
+if ($ARGV[0]) {@N = @ARGV;} else {@N = (1, 2, 2)};
+
+sub rule_two {
+ my $change = undef;
+ my @rank = @{$_[0]};
+ my @candies = @{$_[1]};
+
+ if ($rank[0] > $rank[1] and $candies[0] <= $candies[1]) {
+ $candies[0]++;
+ $change = 1;
+ }
+
+ for (1..$#rank-1) {
+ if (($candies[$_] <= $candies[$_-1] && $rank[$_] > $rank[$_-1] ) ||
+ ($candies[$_] <= $candies[$_+1] && $rank[$_] > $rank[$_+1] ) ) {
+ $candies[$_]++;
+ $change = 1;
+ }
+ }
+
+ if ( $rank[$#rank] > $rank[$#rank-1] and
+ $candies[$#rank] <= $candies[$#rank-1]) {
+ $candies[$#rank]++;
+ $change = 1;
+ }
+
+ return ($change,@candies);
+}
+
+sub countcandies {
+ my @rank = @_;
+ my $bool_ctd;
+ my @candies = (1) x scalar @rank; #apply rule 1
+ do { #apply rule 2 in loop
+ ($bool_ctd,@candies) = rule_two(\@rank, \@candies);
+ } while ($bool_ctd);
+ return sum @candies;
+}
+
+say countcandies(@N);
+
+=pod
+ok countcandies(1, 2, 2) == 4, "pass test case Example 1";
+ok countcandies(1, 4, 3, 2) == 7, "pass test case Example 2";
+ok countcandies(1, 2, 3) == 6, "pass test case 1 2 3";
+ok countcandies(5, 2, 2, 1) == 6, "pass test case 2";
+ok countcandies(1, 8, 1) == 4, "pass test case 3";
+ok countcandies(2, 1, 1) == 4, "pass test case 4";
+ok countcandies(1, 6, 7, 28, 5, 1, 2, 3) == 18, "pass test case 5";
+=cut