From f8a272193042105d79460421c56c4f9e1e392fcb Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Sat, 3 Oct 2020 10:14:09 +0800 Subject: Perl scripts for Task 1 and 2 --- challenge-080/cheok-yin-fung/perl/ch-1.pl | 34 ++++++++++++++++++ challenge-080/cheok-yin-fung/perl/ch-2.pl | 59 +++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 challenge-080/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-080/cheok-yin-fung/perl/ch-2.pl 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 -- cgit