diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2023-09-27 13:44:45 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2023-09-27 13:44:45 -0400 |
| commit | 70d383289a87a38a9fc7cd41febe14602bf1f837 (patch) | |
| tree | f5263cd9aa5607d7e7ef34d7861e087406fd1622 | |
| parent | 3241d3ec32642e07ec394aedd105251d945c900d (diff) | |
| parent | 200e728ec7b29b6f1807ed966264407834eb9f92 (diff) | |
| download | perlweeklychallenge-club-70d383289a87a38a9fc7cd41febe14602bf1f837.tar.gz perlweeklychallenge-club-70d383289a87a38a9fc7cd41febe14602bf1f837.tar.bz2 perlweeklychallenge-club-70d383289a87a38a9fc7cd41febe14602bf1f837.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
104 files changed, 6883 insertions, 2507 deletions
diff --git a/challenge-236/e-choroba/perl/ch-1.pl b/challenge-236/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..4d2b2e34be --- /dev/null +++ b/challenge-236/e-choroba/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub exact_change(@bills) { + my %change = map +($_ => 0), 5, 10; + for my $bill (@bills) { + if ($bill == 10) { + return unless $change{5}--; + + } elsif ($bill == 20) { + # First try to return 10 + 5, we might need 5s later. + if ($change{10} && $change{5}) { + --$change{$_} for 5, 10; + } elsif ($change{5} > 2) { + $change{5} -= 3; + } else { + return + } + next # 20s are never returned. + } + + ++$change{$bill}; + } + return 1 +} + +use Test::More tests => 3 + 1; + +ok exact_change(5, 5, 5, 10, 20), 'Example 1'; +ok ! exact_change(5, 5, 10, 10, 20), 'Example 2'; +ok exact_change(5, 5, 5, 20), 'Example 3'; + +ok exact_change(5, 5, 5, 5, 10, 20, 10), 'Keep the fives'; diff --git a/challenge-236/e-choroba/perl/ch-2.pl b/challenge-236/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..9a06826a7b --- /dev/null +++ b/challenge-236/e-choroba/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub array_loops(@ints) { + my $count; + my @visited; + for my $i (0 .. $#ints) { + next if $visited[$i]; + + # Found a new loop. Visit all the members. + ++$count; + while (! $visited[$i]) { + $visited[$i] = 1; + $i = $ints[$i]; + } + } + return $count +} + +use Test::More tests => 3; + +is array_loops(4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, + 1, 12, 2, 9, 10), + 3, 'Example 1'; +is array_loops(0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, + 5, 3, 18, 15, 19), + 6, 'Example 2'; +is array_loops(9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, + 1, 0, 15, 6, 17), + 1, 'Example 3'; diff --git a/challenge-236/eric-cheung/python/ch-1.py b/challenge-236/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..87c8692ed0 --- /dev/null +++ b/challenge-236/eric-cheung/python/ch-1.py @@ -0,0 +1,29 @@ +
+## arrInputBill = [5, 5, 5, 10, 20] ## Example 1
+## arrInputBill = [5, 5, 10, 10, 20] ## Example 2
+arrInputBill = [5, 5, 5, 20] ## Example 3
+
+arrChange = [0, 0, 0]
+bSucceed = True
+
+for nIndx in range(len(arrInputBill)):
+ if arrInputBill[nIndx] == 5:
+ arrChange[0] = arrChange[0] + 1
+ elif arrInputBill[nIndx] == 10:
+ if arrChange[0] < 1:
+ bSucceed = False
+ break
+ arrChange[0] = arrChange[0] - 1
+ arrChange[1] = arrChange[1] + 1
+ elif arrInputBill[nIndx] == 20:
+ if arrChange[0] < 3 and (arrChange[0] < 1 or arrChange[1] < 1):
+ bSucceed = False
+ break
+ arrChange[2] = arrChange[2] + 1
+ if arrChange[1] > 0:
+ arrChange[1] = arrChange[1] - 1
+ arrChange[0] = arrChange[0] - 1
+ else:
+ arrChange[0] = arrChange[0] - 3
+
+print (bSucceed)
diff --git a/challenge-236/eric-cheung/python/ch-2.py b/challenge-236/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..c503507ba1 --- /dev/null +++ b/challenge-236/eric-cheung/python/ch-2.py @@ -0,0 +1,24 @@ +
+arrInput = [4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10] ## Example 1
+## arrInput = [0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 19] ## Example 2
+## arrInput = [9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17] ## Example 3
+
+arrIndx = [0] * len(arrInput)
+arrOutput = []
+
+for nIndxLoop in range(len(arrIndx)):
+ if arrIndx[nIndxLoop] == 1:
+ continue
+
+ arrTemp = []
+ nTempIndxLoop = nIndxLoop
+
+ while arrInput[nTempIndxLoop] not in arrTemp:
+ arrTemp.append(arrInput[nTempIndxLoop])
+ arrIndx[nTempIndxLoop] = 1
+ nTempIndxLoop = arrInput[nTempIndxLoop]
+
+ arrOutput.append(arrTemp)
+
+## print (arrOutput)
+print (len(arrOutput))
diff --git a/challenge-236/jaldhar-h-vyas/blog.txt b/challenge-236/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..a4390a3686 --- /dev/null +++ b/challenge-236/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2023/09/perl_weekly_challenge_week_236.html diff --git a/challenge-236/jaldhar-h-vyas/perl/ch-1.pl b/challenge-236/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..abd29876b7 --- /dev/null +++ b/challenge-236/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl +use 5.030; +use warnings; +use experimental qw/ switch /; + +sub nochange { + say 'false'; + exit(0); +} + +my $fives = 0; +my $tens = 0; +my $twenties = 0; + +for my $bill (@ARGV) { + given ($bill) { + when (5) { + $fives++; + } + + when (10) { + if ($fives > 0) { + $fives--; + $tens++; + } else { + nochange(); + } + + } + + when (20) { + if ($tens > 0 && $fives > 0) { + $tens -= 1; + $fives -= 1; + $twenties += 1; + } elsif ($fives > 2) { + $fives -= 3; |
