aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-29 05:57:47 +0100
committerGitHub <noreply@github.com>2020-09-29 05:57:47 +0100
commit71fa4472d6473d06d65694c94519b84d4f6bd0af (patch)
treee2c973e8834dac4e305d0194938dd1f471e38ab1
parent3358c7f0aeb3f42e8a5615e4823e33e85433e5b5 (diff)
parent28c499109e28ed567c8c05421ecdd231acba3879 (diff)
downloadperlweeklychallenge-club-71fa4472d6473d06d65694c94519b84d4f6bd0af.tar.gz
perlweeklychallenge-club-71fa4472d6473d06d65694c94519b84d4f6bd0af.tar.bz2
perlweeklychallenge-club-71fa4472d6473d06d65694c94519b84d4f6bd0af.zip
Merge pull request #2398 from PerlBoy1967/branch-for-challenge-080
Task 1 & 2
-rwxr-xr-xchallenge-080/perlboy1967/perl/ch-1.pl40
-rwxr-xr-xchallenge-080/perlboy1967/perl/ch-2.pl41
2 files changed, 81 insertions, 0 deletions
diff --git a/challenge-080/perlboy1967/perl/ch-1.pl b/challenge-080/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..dcf18e42e8
--- /dev/null
+++ b/challenge-080/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 080
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/
+#
+# Task 1 - Smallest Positive Number Bits
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use strict;
+use warnings;
+
+use List::MoreUtils qw(uniq each_array);
+
+@ARGV = qw(-2 0 1 2 3 6 -1 8)
+ unless (scalar @ARGV);
+
+my (@N) = @ARGV;
+
+die "Please supply list of minimal two integer values (negative numbers and zero are allowed)"
+ unless (scalar @N >= 2 and not grep( !m/^[-]*\d+$/, @N));
+
+printf "Input: \@N = (%s)\n", join(', ', @N);
+printf "Output: %s\n", findSmallestPositiveMissing(\@N);
+
+sub findSmallestPositiveMissing {
+ my ($ar) = @_;
+
+ my @p1 = uniq sort { $a <=> $b } grep { $_ >= 0 } @$ar;
+ my @p2 = @p1[1 .. scalar(@p1) - 1];
+
+ my $ea = each_array(@p1, @p2);
+ while (my ($p1, $p2) = $ea->()) {
+ if (defined $p2 and $p1 < $p2 - 1) {
+ return $p1 + 1;
+ }
+ }
+
+ return 'Undef';
+}
diff --git a/challenge-080/perlboy1967/perl/ch-2.pl b/challenge-080/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..9efc45a570
--- /dev/null
+++ b/challenge-080/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 080
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/
+#
+# Task 2 - Count Candies
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use strict;
+use warnings;
+
+use List::Util qw(sum);
+
+@ARGV = qw(1 4 3 2)
+ unless (scalar @ARGV);
+
+my (@N) = @ARGV;
+
+die "Please supply list of minimal two positive integer values"
+ unless (scalar @N >= 2 and not grep( !m/^[-]*\d+$/, @N));
+
+printf "Input: \@N = (%s)\n", join(', ', @N);
+printf "Output: %s\n", countCandies(@N);
+
+sub countCandies {
+ my (@r) = @_;
+
+ # Give each candidate one initial candy
+ my @c = map { 1 } @r;
+
+ # Give additional candy based on ranking v.s. neighbours
+ foreach my $i (0 .. scalar(@r) - 1) {
+ $c[$i]++ if (defined $r[$i + 1] and
+ $r[$i] > $r[$i + 1]);
+ $c[$i]++ if ($i > 0 and
+ $r[$i] > $r[$i - 1]);
+ }
+
+ return sum(@c);
+}