From 28c499109e28ed567c8c05421ecdd231acba3879 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 28 Sep 2020 14:13:19 +0000 Subject: Task 1 & 2 --- challenge-080/perlboy1967/perl/ch-1.pl | 40 +++++++++++++++++++++++++++++++++ challenge-080/perlboy1967/perl/ch-2.pl | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100755 challenge-080/perlboy1967/perl/ch-1.pl create mode 100755 challenge-080/perlboy1967/perl/ch-2.pl 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); +} -- cgit