From d26aed3537962bc1323a7c791bee6eac82910392 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Sun, 1 Oct 2023 09:32:45 +0800 Subject: Week 236 --- challenge-236/cheok-yin-fung/perl/ch-1.pl | 49 +++++++++++++++++++++++++++++++ challenge-236/cheok-yin-fung/perl/ch-2.pl | 33 +++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 challenge-236/cheok-yin-fung/perl/ch-1.pl create mode 100755 challenge-236/cheok-yin-fung/perl/ch-2.pl diff --git a/challenge-236/cheok-yin-fung/perl/ch-1.pl b/challenge-236/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..b5039ea111 --- /dev/null +++ b/challenge-236/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,49 @@ +# The Weekly Challenge 236 +# Task 1 Excat Change +use v5.30; +use warnings; +use List::Util qw/all/; + +sub ec { + my @bills = @_; + die "Bills not in presumed conditions.\n" + unless all {$_ == 5 || $_ == 10 ||$_ == 20} @bills; + my $i = 0; + my $fNotes = 0; + my $dNotes = 0; + my $zNotes = 0; + while ($i <= $#bills) { + my $input = $bills[$i]; + $fNotes += 1 if $input == 5; + if ($input == 10) { + if ($fNotes >= 1) { + $fNotes -= 1; + $dNotes += 1; + } + else { + return 0; + } + } + if ($input == 20) { + if ($fNotes >= 1 && $dNotes >= 1) { + $dNotes -= 1; + $fNotes -= 1; + $zNotes += 1; + } + elsif ($fNotes >= 3) { + $fNotes -= 3; + $zNotes += 1; + } + else { + return 0; + } + } + $i++; + } + return 1; +} + +use Test::More tests=>3; +ok ec(5, 5, 5, 10, 20); +ok !ec(5, 5, 10, 10, 20); +ok ec(5, 5, 5, 20); diff --git a/challenge-236/cheok-yin-fung/perl/ch-2.pl b/challenge-236/cheok-yin-fung/perl/ch-2.pl new file mode 100755 index 0000000000..4a9675e7c2 --- /dev/null +++ b/challenge-236/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,33 @@ +# The Weekly Challenge 236 +# Task 2 Array Loops +use v5.30; +use warnings; + +sub al { + my @ints = @_; + my $loop = 0; + my @traversed; + for my $i (0..$#ints) { + $traversed[$i] = 0; + } + for my $i (0..$#ints) { + next if $traversed[$i]; + my $k_0 = $i; + # print $k_0; + $traversed[$k_0] = 1; + my $k = $ints[$k_0]; + while ($k != $k_0) { + $traversed[$k] = 1; + # print " ", $k; + $k = $ints[$k]; + } + # say ""; + $loop++; + } + return $loop; +} + +use Test::More tests=>3; +ok al(4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10)==3; +ok al(0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19)==6; +ok al(9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17)==1; -- cgit