diff options
| author | Jaldhar H. Vyas <jaldhar@braincells.com> | 2023-09-25 19:02:09 -0400 |
|---|---|---|
| committer | Jaldhar H. Vyas <jaldhar@braincells.com> | 2023-09-25 19:02:09 -0400 |
| commit | 5f1eb30fdf37051c0753f536656ac54cf34274bd (patch) | |
| tree | 8cee1c472e4cba21f7f9518aca529d8c97f18061 | |
| parent | 24b9a96053d234b811652a92f85cc4020786ee74 (diff) | |
| download | perlweeklychallenge-club-5f1eb30fdf37051c0753f536656ac54cf34274bd.tar.gz perlweeklychallenge-club-5f1eb30fdf37051c0753f536656ac54cf34274bd.tar.bz2 perlweeklychallenge-club-5f1eb30fdf37051c0753f536656ac54cf34274bd.zip | |
Challenge 236 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-236/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-236/jaldhar-h-vyas/perl/ch-1.pl | 51 | ||||
| -rwxr-xr-x | challenge-236/jaldhar-h-vyas/perl/ch-2.pl | 26 | ||||
| -rwxr-xr-x | challenge-236/jaldhar-h-vyas/raku/ch-1.raku | 52 | ||||
| -rwxr-xr-x | challenge-236/jaldhar-h-vyas/raku/ch-2.raku | 27 |
5 files changed, 157 insertions, 0 deletions
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; + $twenties += 1; + } else { + nochange(); + } + + } + + default { + die "illegal bill value\n"; + } + } +} + +say "true"; diff --git a/challenge-236/jaldhar-h-vyas/perl/ch-2.pl b/challenge-236/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..0ca8bdf236 --- /dev/null +++ b/challenge-236/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +my @ints = @ARGV; +my %seen; +my $loops = 1; +my $current = 0; + +while (scalar keys %seen != scalar @ints) { + if (exists $seen{$current}) { + $loops++; + + for my $i (0 .. scalar @ints - 1) { + if (!exists $seen{$i}) { + $current = $i; + last; + } + } + } + + $seen{$current} = 1; + $current = $ints[$current]; +} + +say $loops; diff --git a/challenge-236/jaldhar-h-vyas/raku/ch-1.raku b/challenge-236/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..97d7c91153 --- /dev/null +++ b/challenge-236/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,52 @@ +#!/usr/bin/raku + +sub nochange { + say 'false'; + exit(0); +} + +sub MAIN( + *@bills +) { + my $fives = 0; + my $tens = 0; + my $twenties = 0; + + for @bills -> $bill { + 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; + $twenties += 1; + } else { + nochange(); + } + + } + + default { + die "illegal bill value\n"; + } + } + } + + say "true"; +}
\ No newline at end of file diff --git a/challenge-236/jaldhar-h-vyas/raku/ch-2.raku b/challenge-236/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..9d81667d1c --- /dev/null +++ b/challenge-236/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,27 @@ +#!/usr/bin/raku + +sub MAIN( + *@ints +) { + my %seen; + my $loops = 1; + my $current = 0; + + while %seen.keys.elems != @ints.elems { + if %seen{$current}:exists { + $loops++; + + for 0 .. @ints.end -> $i { + if %seen{$i}:!exists { + $current = $i; + last; + } + } + } + + %seen{$current} = True; + $current = @ints[$current]; + } + + say $loops; +}
\ No newline at end of file |
