From a04070a3510a655b591c20e180fd70815b66915a Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Wed, 20 Sep 2023 11:43:23 -0400 Subject: DAJ 235 blog --- challenge-235/dave-jacoby/blog.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 challenge-235/dave-jacoby/blog.txt diff --git a/challenge-235/dave-jacoby/blog.txt b/challenge-235/dave-jacoby/blog.txt new file mode 100644 index 0000000000..92c92df39c --- /dev/null +++ b/challenge-235/dave-jacoby/blog.txt @@ -0,0 +1,2 @@ +https://jacoby.github.io/2023/09/20/there-goes-my-zero-weekly-challenge-235.html + -- cgit From 49457a0c22fca1323966fd21850bd6bcaf62a13c Mon Sep 17 00:00:00 2001 From: deoac Date: Mon, 25 Sep 2023 16:29:52 -0400 Subject: Initial commit --- challenge-236/shimon-ben-avraham/raku/ch-1.md | 178 +++++++++++++++++++ challenge-236/shimon-ben-avraham/raku/ch-1.sl | 133 +++++++++++++++ challenge-236/shimon-ben-avraham/raku/ch-2.md | 188 +++++++++++++++++++++ challenge-236/shimon-ben-avraham/raku/ch-2.sl | 141 ++++++++++++++++ .../raku/perl-weekly-challenge.pod | 97 +++++++++++ 5 files changed, 737 insertions(+) create mode 100644 challenge-236/shimon-ben-avraham/raku/ch-1.md create mode 100755 challenge-236/shimon-ben-avraham/raku/ch-1.sl create mode 100644 challenge-236/shimon-ben-avraham/raku/ch-2.md create mode 100755 challenge-236/shimon-ben-avraham/raku/ch-2.sl create mode 100644 challenge-236/shimon-ben-avraham/raku/perl-weekly-challenge.pod diff --git a/challenge-236/shimon-ben-avraham/raku/ch-1.md b/challenge-236/shimon-ben-avraham/raku/ch-1.md new file mode 100644 index 0000000000..8d8f5c0b5f --- /dev/null +++ b/challenge-236/shimon-ben-avraham/raku/ch-1.md @@ -0,0 +1,178 @@ +# Challenge # 236 Task 1, Exact Change +> +## Table of Contents +[Submitted by: Mohammad S Anwar](#submitted-by-mohammad-s-anwar) +[The Challenge](#the-challenge) +[Example 1](#example-1) +[Example 2](#example-2) +[Example 3](#example-3) +[The Solution](#the-solution) +[TITLE](#title) +[VERSION](#version) +[SYNOPSIS](#synopsis) +[REQUIRED ARGUMENTS](#required-arguments) +[OPTIONS](#options) +[DESCRIPTION](#description) +[DIAGNOSTICS](#diagnostics) +[CONFIGURATION AND ENVIRONMENT](#configuration-and-environment) +[DEPENDENCIES](#dependencies) +[INCOMPATIBILITIES](#incompatibilities) +[BUGS AND LIMITATIONS](#bugs-and-limitations) +[AUTHOR](#author) +[LICENCE AND COPYRIGHT](#licence-and-copyright) + +---- +# Submitted by: Mohammad S Anwar +# The Challenge +You are asked to sell juice each costs $5. You are given an array of bills. You can only sell ONE juice to each customer but make sure you return exact change back. You only have $5, $10 and $20 notes. You do not have any change in hand at first. + +Write a script to find out if it is possible to sell to each customers with correct change. + +## Example 1 +``` +Input: @bills = (5, 5, 5, 10, 20) +Output: true + +From the first 3 customers, we collect three $5 bills in order. +From the fourth customer, we collect a $10 bill and give back a $5. +From the fifth customer, we give a $10 bill and a $5 bill. +Since all customers got correct change, we output true. + +``` +## Example 2 +``` +Input: @bills = (5, 5, 10, 10, 20) +Output: false + +From the first two customers in order, we collect two $5 bills. +For the next two customers in order, we collect a $10 bill and give back a $5 bill. +For the last customer, we can not give the change of $15 back because we only have two $10 bills. +Since not every customer received the correct change, the answer is false. + + +``` +## Example 3 +``` +Input: @bills = (5, 5, 5, 20) +Output: true + + +``` +# The Solution + + + + +``` + 3| multi MAIN ( ) { + 4| ; + 5| } + +``` + + + + +# TITLE + - + +# VERSION +This documentation refers to version 0.0.1 + +# SYNOPSIS +``` +# Brief working invocation example(s) here showing the most common usage(s) + +# This section will be as far as many users ever read +# so make it as educational and exemplary as possible. +``` +# REQUIRED ARGUMENTS +A complete list of every argument that must appear on the command line. when the application is invoked, explaining what each of them does, any restrictions on where each one may appear (i.e. flags that must appear before or after filenames), and how the various arguments and options may interact (e.g. mutual exclusions, required combinations, etc.) + +If all of the application's arguments are optional this section may be omitted entirely. + +# OPTIONS +A complete list of every available option with which the application can be invoked, explaining what each does, and listing any restrictions, or interactions. + +If the application has no options this section may be omitted entirely. + +# DESCRIPTION +A full description of the application and its features. May include numerous subsections (i.e. =head2, =head3, etc.) + +# DIAGNOSTICS +A list of every error and warning message that the application can generate (even the ones that will "never happen"), with a full explanation of each problem, one or more likely causes, and any suggested remedies. If the application generates exit status codes (e.g. under Unix) then list the exit status associated with each error. + +# CONFIGURATION AND ENVIRONMENT +A full explanation of any configuration system(s) used by the application, including the names and locations of any configuration files, and the meaning of any environment variables or properties that can be set. These descriptions must also include details of any configuration language used + +# DEPENDENCIES +A list of all the other modules that this module relies upon, including any restrictions on versions, and an indication whether these required modules are part of the standard Perl distribution, part of the module's distribution, or must be installed separately. + +# INCOMPATIBILITIES +A list of any modules that this module cannot be used in conjunction with. This may be due to name conflicts in the interface, or competition for system or program resources, or due to internal limitations of Perl (for example, many modules that use source code filters are mutually incompatible). + +# BUGS AND LIMITATIONS +A list of known problems with the module, together with some indication whether they are likely to be fixed in an upcoming release. + +Also a list of restrictions on the features the module does provide: data types that cannot be handled, performance issues and the circumstances in which they may arise, practical limitations on the size of data sets, special cases that are not (yet) handled, etc. + +The initial template usually just has: + +There are no known bugs in this module. + +# AUTHOR +Shimon Bollinger (deoac.shimon@gmail.com) + +Source can be located at: https://github.com/deoac/... . Comments and Pull Requests are welcome. + +# LICENCE AND COPYRIGHT +© 2023 Shimon Bollinger. All rights reserved. + +This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See [perlartistic](http://perldoc.perl.org/perlartistic.html). + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + + + + +``` + 6| multi MAIN (:$test!) { + 7| use Test; + 8| + 9| my @tests = [ + 10| %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + 11| ]; + 12| + 13| for @tests { + 14| } + 15| } + 16| + 17| my %*SUB-MAIN-OPTS = + 18| :named-anywhere, + 19| :bundling, + 20| :allow-no, + 21| :numeric-suffix-as-value, + 22| ; + 23| + 24| multi MAIN(Bool :$pod!) { + 25| for $=pod -> $pod-item { + 26| for $pod-item.contents -> $pod-block { + 27| $pod-block.raku.say; + 28| } + 29| } + 30| } + 31| + 32| multi MAIN(Bool :$doc!, Str :$format = 'Text') { + 33| run $*EXECUTABLE, "--doc=$format", $*PROGRAM; + 34| } + +``` + + + + + + +---- +Rendered from at 2023-09-25T20:12:20Z diff --git a/challenge-236/shimon-ben-avraham/raku/ch-1.sl b/challenge-236/shimon-ben-avraham/raku/ch-1.sl new file mode 100755 index 0000000000..218c1bd76a --- /dev/null +++ b/challenge-236/shimon-ben-avraham/raku/ch-1.sl @@ -0,0 +1,133 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge #236 Task 1 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 25 Sep 2023 04:25:50 PM EDT +# Version 0.0.1 + +# begin-no-weave +# always use the latest version of Raku +use v6.*; +# end-no-weave + +=begin pod +=TITLE Challenge # 236 Task 1, Exact Change + +=head1 Submitted by: Mohammad S Anwar + +=head1 The Challenge + +You are asked to sell juice each costs $5. You are given an array of bills. You +can only sell ONE juice to each customer but make sure you return exact change +back. You only have $5, $10 and $20 notes. You do not have any change in hand +at first. + +Write a script to find out if it is possible to sell to each customers with +correct change. + +=head2 Example 1 + +=begin code :lang +Input: @bills = (5, 5, 5, 10, 20) +Output: true + +From the first 3 customers, we collect three $5 bills in order. +From the fourth customer, we collect a $10 bill and give back a $5. +From the fifth customer, we give a $10 bill and a $5 bill. +Since all customers got correct change, we output true. +=end code + +=head2 Example 2 + +=begin code :lang +Input: @bills = (5, 5, 10, 10, 20) +Output: false + +From the first two customers in order, we collect two $5 bills. +For the next two customers in order, we collect a $10 bill and give back a $5 bill. +For the last customer, we can not give the change of $15 back because we only have two $10 bills. +Since not every customer received the correct change, the answer is false. + +=end code + +=head2 Example 3 +=begin code :lang + +Input: @bills = (5, 5, 5, 20) +Output: true + +=end code + +=head1 The Solution + +=end pod + + +#| The actual program starts here. +multi MAIN ( ) { + ; +} # end of multi MAIN ( ) + + +=begin pod + +=head1 AUTHOR + +Shimon Bollinger (deoac.shimon@gmail.com) + +Source can be located at: https://github.com/deoac/... . Comments and +Pull Requests are welcome. + +=head1 LICENCE AND COPYRIGHT + +© 2023 Shimon Bollinger. All rights reserved. + +This module is free software; you can redistribute it and/or +modify it under the same terms as Perl itself. +See L. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +=end pod + +#| Run with the option '--test' to test the program +multi MAIN (:$test!) { + use Test; + + my @tests = [ + %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + ]; + + for @tests { +# cmp-ok ., ., ., .; + } # end of for @tests +} # end of multi MAIN (:$test!) + +my %*SUB-MAIN-OPTS = + :named-anywhere, # allow named variables at any location + :bundling, # allow bundling of named arguments +# :coerce-allomorphs-to(Str), # coerce allomorphic arguments to given type + :allow-no, # allow --no-foo as alternative to --/foo + :numeric-suffix-as-value, # allow -j2 as alternative to --j=2 +; + +#| Run with '--pod' to see all of the POD6 objects +multi MAIN(Bool :$pod!) { + for $=pod -> $pod-item { + for $pod-item.contents -> $pod-block { + $pod-block.raku.say; + } + } +} # end of multi MAIN (:$pod) + +#| Run with '--doc' to generate a document from the POD6 +#| It will be rendered in Text format +#| unless specified with the --format option. e.g. +#| --format=HTML +multi MAIN(Bool :$doc!, Str :$format = 'Text') { + run $*EXECUTABLE, "--doc=$format", $*PROGRAM; +} # end of multi MAIN(Bool :$man!) + + diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.md b/challenge-236/shimon-ben-avraham/raku/ch-2.md new file mode 100644 index 0000000000..8625d6862c --- /dev/null +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.md @@ -0,0 +1,188 @@ +# Challenge # 236 Task 2, Array Loops +> +## Table of Contents +[Submitted By: Mark Anderson](#submitted-by-mark-anderson) +[The Challenge You are given an array of unique integers.](#the-challenge-you-are-given-an-array-of-unique-integers) +[Example 1](#example-1) +[Example 2](#example-2) +[Example 3](#example-3) +[The Solution](#the-solution) +[TITLE](#title) +[VERSION](#version) +[SYNOPSIS](#synopsis) +[REQUIRED ARGUMENTS](#required-arguments) +[OPTIONS](#options) +[DESCRIPTION](#description) +[DIAGNOSTICS](#diagnostics) +[CONFIGURATION AND ENVIRONMENT](#configuration-and-environment) +[DEPENDENCIES](#dependencies) +[INCOMPATIBILITIES](#incompatibilities) +[BUGS AND LIMITATIONS](#bugs-and-limitations) +[AUTHOR](#author) +[LICENCE AND COPYRIGHT](#licence-and-copyright) + +---- +# Submitted By: Mark Anderson +# The Challenge You are given an array of unique integers. +Write a script to determine how many loops are in the given array. + +> **To determine a loop: Start at an index and take the number at array[index] and then proceed to that index and continue this until you end up at the starting index.** + + +## Example 1 +``` +Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) +Output: 3 + +To determine the 1st loop, start at index 0, the number at that index is 4, +proceed to index 4, the number at that index is 15, proceed to index 15 and so +on until you're back at index 0. + +Loops are as below: +[4 15 1 6 13 5 0] +[3 8 7 18 9 16 12 17 2] +[14 11 19 10] + + +``` +## Example 2 +``` +Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) +Output: 6 + +Loops are as below: +[0] +[1] +[13 9 14 17 18 15 5 8 2] +[7 11 4 6 10 16 3] +[12] +[19] + +``` +## Example 3 +``` +Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) +Output: 1 + +Loop is as below: +[9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] + +``` +# The Solution + + + + +``` + 3| multi MAIN ( ) { + 4| ; + 5| } + +``` + + + + +# TITLE + - + +# VERSION +This documentation refers to version 0.0.1 + +# SYNOPSIS +``` +# Brief working invocation example(s) here showing the most common usage(s) + +# This section will be as far as many users ever read +# so make it as educational and exemplary as possible. +``` +# REQUIRED ARGUMENTS +A complete list of every argument that must appear on the command line. when the application is invoked, explaining what each of them does, any restrictions on where each one may appear (i.e. flags that must appear before or after filenames), and how the various arguments and options may interact (e.g. mutual exclusions, required combinations, etc.) + +If all of the application's arguments are optional this section may be omitted entirely. + +# OPTIONS +A complete list of every available option with which the application can be invoked, explaining what each does, and listing any restrictions, or interactions. + +If the application has no options this section may be omitted entirely. + +# DESCRIPTION +A full description of the application and its features. May include numerous subsections (i.e. =head2, =head3, etc.) + +# DIAGNOSTICS +A list of every error and warning message that the application can generate (even the ones that will "never happen"), with a full explanation of each problem, one or more likely causes, and any suggested remedies. If the application generates exit status codes (e.g. under Unix) then list the exit status associated with each error. + +# CONFIGURATION AND ENVIRONMENT +A full explanation of any configuration system(s) used by the application, including the names and locations of any configuration files, and the meaning of any environment variables or properties that can be set. These descriptions must also include details of any configuration language used + +# DEPENDENCIES +A list of all the other modules that this module relies upon, including any restrictions on versions, and an indication whether these required modules are part of the standard Perl distribution, part of the module's distribution, or must be installed separately. + +# INCOMPATIBILITIES +A list of any modules that this module cannot be used in conjunction with. This may be due to name conflicts in the interface, or competition for system or program resources, or due to internal limitations of Perl (for example, many modules that use source code filters are mutually incompatible). + +# BUGS AND LIMITATIONS +A list of known problems with the module, together with some indication whether they are likely to be fixed in an upcoming release. + +Also a list of restrictions on the features the module does provide: data types that cannot be handled, performance issues and the circumstances in which they may arise, practical limitations on the size of data sets, special cases that are not (yet) handled, etc. + +The initial template usually just has: + +There are no known bugs in this module. + +# AUTHOR +Shimon Bollinger (deoac.shimon@gmail.com) + +Source can be located at: https://github.com/deoac/... . Comments and Pull Requests are welcome. + +# LICENCE AND COPYRIGHT +© 2023 Shimon Bollinger. All rights reserved. + +This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See [perlartistic](http://perldoc.perl.org/perlartistic.html). + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + + + + +``` + 6| multi MAIN (:$test!) { + 7| use Test; + 8| + 9| my @tests = [ + 10| %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + 11| ]; + 12| + 13| for @tests { + 14| } + 15| } + 16| + 17| my %*SUB-MAIN-OPTS = + 18| :named-anywhere, + 19| :bundling, + 20| :allow-no, + 21| :numeric-suffix-as-value, + 22| ; + 23| + 24| multi MAIN(Bool :$pod!) { + 25| for $=pod -> $pod-item { + 26| for $pod-item.contents -> $pod-block { + 27| $pod-block.raku.say; + 28| } + 29| } + 30| } + 31| + 32| multi MAIN(Bool :$doc!, Str :$format = 'Text') { + 33| run $*EXECUTABLE, "--doc=$format", $*PROGRAM; + 34| } + +``` + + + + + + +---- +Rendered from at 2023-09-25T20:20:54Z diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.sl b/challenge-236/shimon-ben-avraham/raku/ch-2.sl new file mode 100755 index 0000000000..3c8a4ad460 --- /dev/null +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.sl @@ -0,0 +1,141 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge #236 Task 2 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 25 Sep 2023 04:22:22 PM EDT +# Version 0.0.1 + +# begin-no-weave +# always use the latest version of Raku +use v6.*; +# end-no-weave + +=begin pod +=TITLE Challenge # 236 Task 2, Array Loops + +=head1 Submitted By: Mark Anderson + +=head1 The Challenge + +You are given an array of unique integers. + +Write a script to determine how many loops are in the given array. + +=defn +To determine a loop: Start at an index and take the number at array[index] and then proceed to that index and continue this until you end up at the starting index. + +=head2 Example 1 + +=begin code :lang +Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) +Output: 3 + +To determine the 1st loop, start at index 0, the number at that index is 4, +proceed to index 4, the number at that index is 15, proceed to index 15 and so +on until you're back at index 0. + +Loops are as below: +[4 15 1 6 13 5 0] +[3 8 7 18 9 16 12 17 2] +[14 11 19 10] + +=end code + +=head2 Example 2 + +=begin code :lang +Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) +Output: 6 + +Loops are as below: +[0] +[1] +[13 9 14 17 18 15 5 8 2] +[7 11 4 6 10 16 3] +[12] +[19] +=end code + +=head2 Example 3 + +=begin code :lang +Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) +Output: 1 + +Loop is as below: +[9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] +=end code + + +=head1 The Solution +=end pod + + +#| The actual program starts here. +multi MAIN ( ) { + ; +} # end of multi MAIN ( ) + + +=begin pod + +=head1 AUTHOR + +Shimon Bollinger (deoac.shimon@gmail.com) + +Source can be located at: https://github.com/deoac/... . Comments and +Pull Requests are welcome. + +=head1 LICENCE AND COPYRIGHT + +© 2023 Shimon Bollinger. All rights reserved. + +This module is free software; you can redistribute it and/or +modify it under the same terms as Perl itself. +See L. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +=end pod + +#| Run with the option '--test' to test the program +multi MAIN (:$test!) { + use Test; + + my @tests = [ + %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + ]; + + for @tests { +# cmp-ok ., ., ., .; + } # end of for @tests +} # end of multi MAIN (:$test!) + +my %*SUB-MAIN-OPTS = + :named-anywhere, # allow named variables at any location + :bundling, # allow bundling of named arguments +# :coerce-allomorphs-to(Str), # coerce allomorphic arguments to given type + :allow-no, # allow --no-foo as alternative to --/foo + :numeric-suffix-as-value, # allow -j2 as alternative to --j=2 +; + +#| Run with '--pod' to see all of the POD6 objects +multi MAIN(Bool :$pod!) { + for $=pod -> $pod-item { + for $pod-item.contents -> $pod-block { + $pod-block.raku.say; + } + } +} # end of multi MAIN (:$pod) + +#| Run with '--doc' to generate a document from the POD6 +#| It will be rendered in Text format +#| unless specified with the --format option. e.g. +#| --format=HTML +multi MAIN(Bool :$doc!, Str :$format = 'Text') { + run $*EXECUTABLE, "--doc=$format", $*PROGRAM; +} # end of multi MAIN(Bool :$man!) + + diff --git a/challenge-236/shimon-ben-avraham/raku/perl-weekly-challenge.pod b/challenge-236/shimon-ben-avraham/raku/perl-weekly-challenge.pod new file mode 100644 index 0000000000..e649f478ed --- /dev/null +++ b/challenge-236/shimon-ben-avraham/raku/perl-weekly-challenge.pod @@ -0,0 +1,97 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge # Task 1 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 25 Sep 2023 04:22:44 PM EDT +# Version 0.0.1 + +# begin-no-weave +# always use the latest version of Raku +use v6.*; +# end-no-weave + +=begin pod +=TITLE Challenge # Task , + +=head1 Submitted by: + +=head1 The Challenge + +=head2 Example 1 + +=begin code :lang +Input: +Output: + +=end code + +=head1 The Solution + +=end pod + +#| The actual program starts here. +multi MAIN ( ) { + ; +} # end of multi MAIN ( ) + + +=begin pod +=head1 AUTHOR + +Shimon Bollinger (deoac.shimon@gmail.com) + +Source can be located at: https://github.com/deoac/... . Comments and +Pull Requests are welcome. + +=head1 LICENCE AND COPYRIGHT + +© 2023 Shimon Bollinger. All rights reserved. + +This module is free software; you can redistribute it and/or +modify it under the same terms as Perl itself. +See L. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +=end pod + +#| Run with the option '--test' to test the program +multi MAIN (:$test!) { + use Test; + + my @tests = [ + %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + ]; + + for @tests { +# cmp-ok ., ., ., .; + } # end of for @tests +} # end of multi MAIN (:$test!) + +my %*SUB-MAIN-OPTS = + :named-anywhere, # allow named variables at any location + :bundling, # allow bundling of named arguments +# :coerce-allomorphs-to(Str), # coerce allomorphic arguments to given type + :allow-no, # allow --no-foo as alternative to --/foo + :numeric-suffix-as-value, # allow -j2 as alternative to --j=2 +; + +#| Run with '--pod' to see all of the POD6 objects +multi MAIN(Bool :$pod!) { + for $=pod -> $pod-item { + for $pod-item.contents -> $pod-block { + $pod-block.raku.say; + } + } +} # end of multi MAIN (:$pod) + +#| Run with '--doc' to generate a document from the POD6 +#| It will be rendered in Text format +#| unless specified with the --format option. e.g. +#| --format=HTML +multi MAIN(Bool :$doc!, Str :$format = 'Text') { + run $*EXECUTABLE, "--doc=$format", $*PROGRAM; +} # end of multi MAIN(Bool :$man!) + -- cgit From b98e7abe19abaf83dee1d77ad68ed382f88601b7 Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Wed, 27 Sep 2023 10:46:38 -0500 Subject: Week 236 solutions with blog links --- challenge-236/bob-lied/README | 6 +- challenge-236/bob-lied/blog.txt | 2 + challenge-236/bob-lied/perl/ch-1.pl | 117 ++++++++++++++++++++++++++++++++++++ challenge-236/bob-lied/perl/ch-2.pl | 89 +++++++++++++++++++++++++++ 4 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 challenge-236/bob-lied/blog.txt create mode 100644 challenge-236/bob-lied/perl/ch-1.pl create mode 100644 challenge-236/bob-lied/perl/ch-2.pl diff --git a/challenge-236/bob-lied/README b/challenge-236/bob-lied/README index 078b56c84e..f17c1f61f3 100644 --- a/challenge-236/bob-lied/README +++ b/challenge-236/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 235 by Bob Lied +Solutions to weekly challenge 236 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-235/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-235/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-236/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-236/bob-lied diff --git a/challenge-236/bob-lied/blog.txt b/challenge-236/bob-lied/blog.txt new file mode 100644 index 0000000000..9a5af4b168 --- /dev/null +++ b/challenge-236/bob-lied/blog.txt @@ -0,0 +1,2 @@ +https://dev.to/boblied/pwc-236-task-1-a-change-would-do-you-good-3ia4 +https://dev.to/boblied/pwc-236-task-2-the-circle-is-small-242g diff --git a/challenge-236/bob-lied/perl/ch-1.pl b/challenge-236/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..2159d40e4d --- /dev/null +++ b/challenge-236/bob-lied/perl/ch-1.pl @@ -0,0 +1,117 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge 236 Task 1 Exact Change +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are asked to sell juice each costs $5. You are given an array of bills. +# You can only sell ONE juice to each customer but make sure you return exact +# change back. You only have $5, $10 and $20 notes. You do not have any +# change in hand at first. +# +# Write a script to find out if it is possible to sell to each customers +# with correct change. +# +# Example 1 Input: @bills = (5, 5, 5, 10, 20) +# Output: true +# From the first 3 customers, we collect three $5 bills in order. +# From the fourth customer, we collect a $10 bill and give back a $5. +# From the fifth customer, we give a $10 bill and a $5 bill. +# Since all customers got correct change, we output true. +# +# Example 2 Input: @bills = (5, 5, 10, 10, 20) +# Output: false +# From the first two customers in order, we collect two $5 bills. +# For the next two customers in order, we collect a $10 bill and give +# back a $5 bill. +# For the last customer, we can not give the change of $15 back because +# we only have two $10 bills. +# Since not every customer received the correct change, the answer is false. +# +# Example 3 Input: @bills = (5, 5, 5, 20) +# Output: true +#============================================================================= + +use v5.38; +use builtin qw/true false/; no warnings 'experimental::builtin'; + +$" = ', '; # LIST_SEPARATOR, for printing an array in verbose tracing +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say exactChange(@ARGV) ? "true" : "false"; + +sub exactChange(@bills) +{ +use constant { FIVE => 0, TEN => 1, TWENTY => 2 }; + my @register = ( 0, 0, 0 ); + my $changeGiven = true; + while ( (my $payment = shift @bills) && $changeGiven ) + { + if ( $payment == 5 ) + { + say "Got a 5, register=(@register)" if $Verbose; + $register[FIVE]++; + } + elsif ( $payment == 10 ) + { + say "Got a 10, register=(@register)" if $Verbose; + if ( $register[FIVE] ) + { + say " Gave change 5" if $Verbose; + $register[FIVE]--; + } + else + { + say " NO 5, FAIL" if $Verbose; + $changeGiven = false; + } + $register[TEN]++; + } + elsif ( $payment == 20 ) + { + say "Got a 20, register=(@register)" if $Verbose; + if ( $register[TEN] && $register[FIVE] ) + { + say " Give a 5 and a 10" if $Verbose; + $register[TEN]--; + $register[FIVE]--; + } + elsif ( $register[FIVE] >= 3 ) + { + say " Gave three 5s" if $Verbose; + $register[FIVE] -= 3; + } + else + { + say " No 15, FAIL" if $Verbose; + $changeGiven = false + } + $register[TWENTY]++; + } + else + { + say "Got a fake bill" if $Verbose; + } + } + say "DONE: register:(@register) customer=(@bills) change=$changeGiven" if $Verbose; + return @bills == 0 && $changeGiven; +} + +sub runTest +{ + use Test2::V0; + no warnings 'experimental::builtin'; + + is( exactChange(5,5,5,10,20), true, "Example 1"); + is( exactChange(5,5,10,10,20), false, "Example 2"); + is( exactChange(5,5,5,20), true, "Example 3"); + + done_testing; +} diff --git a/challenge-236/bob-lied/perl/ch-2.pl b/challenge-236/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..ad023abb02 --- /dev/null +++ b/challenge-236/bob-lied/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge Task 2 Array Loops +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given an array of unique integers. +# Write a script to determine how many loops are in the given array. +# To determine a loop: Start at an index and take the number at array[index] +# and then proceed to that index and continue this until you end up at the +# starting index. +# Example 1 Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) +# Output: 3 +# To determine the 1st loop, start at index 0, the number at that +# index is 4, proceed to index 4, the number at that index is 15, +# proceed to index 15 and so on until you're back at index 0. +# Loops are as below: +# [4 15 1 6 13 5 0] +# [3 8 7 18 9 16 12 17 2] +# [14 11 19 10] +# Example 2 Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) +# Output: 6 +# [0] +# [1] +# [13 9 14 17 18 15 5 8 2] +# [7 11 4 6 10 16 3] +# [12] +# [19] +# Example 3 Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) +# Output: 1 +# [9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] +###### +# The examples imply that the ints are actually a permutation of the +# indices 0..$#ints. That means there will always be at least one loop, +# and every element will be part of a loop. +#============================================================================= + +use v5.38; +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say arrayLoops(@ARGV); + +sub arrayLoops(@ints) +{ + my @isInAnyLoop = (false) x @ints; + + my $loopCount = 0; + for ( my $start = 0 ; $start <= $#ints ; $start++ ) + { + next if $isInAnyLoop[$start]; + + my @visited = (false) x @ints; + $visited[$start] = true; + my @loop = ( $ints[$start] ); + + my $next = $ints[$start]; + while ( ! $visited[$next] ) + { + $isInAnyLoop[$next] = $visited[$next] = true; + push @loop, $ints[$next] if $Verbose; + $next = $ints[$next]; + } + $loopCount++; + say "LOOP: [@loop]" if $Verbose; + } + return $loopCount; +} + +sub runTest +{ + use Test2::V0; + + is( arrayLoops( 4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10), + 3, "Example 1"); + is( arrayLoops( 0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19), + 6, "Example 2"); + is( arrayLoops( 9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17), + 1, "Example 3"); + + done_testing; +} -- cgit From 09be60a52dbe06ea28f8bb835b31e37d3ea93fa5 Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Wed, 27 Sep 2023 18:00:56 +0100 Subject: add solutions week 236 in python --- challenge-236/steven-wilson/python/ch-01.py | 56 +++++++++++++++++++++++++++++ challenge-236/steven-wilson/python/ch-02.py | 34 ++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 challenge-236/steven-wilson/python/ch-01.py create mode 100644 challenge-236/steven-wilson/python/ch-02.py diff --git a/challenge-236/steven-wilson/python/ch-01.py b/challenge-236/steven-wilson/python/ch-01.py new file mode 100644 index 0000000000..8bea4269ad --- /dev/null +++ b/challenge-236/steven-wilson/python/ch-01.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +JUICE_PRICE = 5 + + +def correct_change_to_each(transactions): + """ Can correct change be given to each customer + >>> correct_change_to_each([5, 5, 5, 10, 20]) + True + >>> correct_change_to_each([5, 5, 10, 10, 20]) + False + >>> correct_change_to_each([5, 5, 5, 20]) + True + """ + in_hand = [] + for transaction in transactions: + if transaction > JUICE_PRICE: + change = transaction - JUICE_PRICE + bills = correct_change(change, in_hand) + if bills == []: + return False + else: + for bill in bills: + in_hand.remove(bill) + in_hand.append(transaction) + return True + + +def correct_change(change, bills): + """ Returns the bills required to give correct change or empty list + if not possible + >>> correct_change(5, [5, 5, 5]) + [5] + >>> correct_change(15, [5, 5, 10]) + [5, 10] + >>> correct_change(15, [10, 10]) + [] + >>> correct_change(15, [5, 5, 5]) + [5, 5, 5] + >>> correct_change(10, []) + [] + """ + give = [] + bills.sort(reverse=True) + for bill in bills: + if (change - bill + sum(give)) >= 0: + give.append(bill) + if (sum(give) == change): + return sorted(give) + return [] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/challenge-236/steven-wilson/python/ch-02.py b/challenge-236/steven-wilson/python/ch-02.py new file mode 100644 index 0000000000..f854671e81 --- /dev/null +++ b/challenge-236/steven-wilson/python/ch-02.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + + +def array_loops(elements): + ''' determine how many loops are in the given array + >>> array_loops([4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10]) + 3 + >>> array_loops([0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19]) + 6 + >>> array_loops([9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17]) + 1 + ''' + loops = [] + seen = [] + for index, value in enumerate(elements): + if index in seen: + continue + start = index + loop = [] + index = elements[index] + while(start != index): + loop.append(index) + seen.append(index) + index = elements[index] + loop.append(start) + seen.append(start) + loops.append(loop) + return len(loops) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() -- cgit From 3241d3ec32642e07ec394aedd105251d945c900d Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Wed, 27 Sep 2023 13:27:24 -0400 Subject: DAJ 236 no-blog --- challenge-236/dave-jacoby/perl/ch-1.pl | 64 ++++++++++++++++++++++++++++++++++ challenge-236/dave-jacoby/perl/ch-2.pl | 54 ++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 challenge-236/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-236/dave-jacoby/perl/ch-2.pl diff --git a/challenge-236/dave-jacoby/perl/ch-1.pl b/challenge-236/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..7d2163e399 --- /dev/null +++ b/challenge-236/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::MoreUtils qw( first_index ); +use List::Util qw( sum0 ); + +my @examples = ( + + [ 5, 5, 5, 10, 20 ], + [ 5, 5, 10, 10, 20 ], + [ 5, 5, 5, 20 ], +); + +for my $e (@examples) { + my @ints = $e->@*; + my $ints = join ', ', @ints; + my $output = exact_change(@ints) ? 'true' : 'false'; + say <<~"END"; + Input: \@ints = ($ints) + Output: $output + END +} + +sub exact_change (@transactions) { + my @till; + my $till = 0; +T: for my $t (@transactions) { + my $change = $t - 5; + $till += 5; + push @till, $t; + if ($change) { + my @bills = has_change( $change, \@till, [] ); + my $bills = sum0 @bills; + return 0 if $change != $bills; + for my $b (@bills) { + my $fi = first_index { $_ == $b } @till; + delete $till[$fi]; + @till = grep { defined } @till; + } + } + } + return 1; +} + +sub has_change ( $change, $till, $values = [] ) { + my @till = sort { $b <=> $a } $till->@*; # sort big to small, big bills first + my $sum = sum0 $values->@*; + return if $sum > $change; # too much change + return if !scalar @till; # not enough in till + return $values->@* if $sum == $change; # exactly right + for my $i ( 0 .. -1 + scalar @till ) { # + my @copy = $values->@*; + my $v = shift @till; + push @copy, $v; + my @out = has_change( $change, \@till, \@copy ); + my $val = sum0 @out; + return @out if $val == $change; + push @till, $v; + } + return; +} diff --git a/challenge-236/dave-jacoby/perl/ch-2.pl b/challenge-236/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..5bbc57eb5c --- /dev/null +++ b/challenge-236/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ uniq }; + +my @examples = ( + + [ 4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10 ], + [ 0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 19 ], + [ 9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17 ], +); + +for my $e (@examples) { + my @ints = $e->@*; + my $ints = join ', ', @ints; + my $output = find_loops( \@ints ); + say <<~"END"; + Input: \@ints = + ($ints) + Output: $output + END +} + +sub find_loops ($ints) { + my $output = 0; + my %no_go; + for my $i ( 0 .. -1 + scalar $ints->@* ) { + my $v = $ints->[$i]; + next if $no_go{$v}; + my @loop = ($v); + my $loop = traverse_loop( $ints, \@loop ); + if ( $loop == -1 ) { } + if ( scalar $loop->@* ) { + # say qq{has_loop: }. join ', ', $loop->@*; + map { $no_go{$_} = 1 } $loop->@*; + $output++; + } + } + return $output; +} + +sub traverse_loop ( $ints, $loop ) { + my $first = $loop->[0]; + my $last = $loop->[-1]; + my $next = $ints->[$last]; + if ( scalar $loop->@* > scalar $ints->@* ) { return -1 } + if ( $next == $first ) { return $loop } + my $copy->@* = $loop->@*; + push $copy->@*, $next; + return traverse_loop( $ints, $copy ); +} -- cgit From 9eb3d83e260160d1b43ebdecda89fc64ebc7a46d Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Wed, 27 Sep 2023 20:15:02 +0000 Subject: w236 - Task 1 & 2 --- challenge-236/perlboy1967/perl/ch1.pl | 58 +++++++++++++++++++++++++++++++++++ challenge-236/perlboy1967/perl/ch2.pl | 51 ++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100755 challenge-236/perlboy1967/perl/ch1.pl create mode 100755 challenge-236/perlboy1967/perl/ch2.pl diff --git a/challenge-236/perlboy1967/perl/ch1.pl b/challenge-236/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..c0d8f73b9f --- /dev/null +++ b/challenge-236/perlboy1967/perl/ch1.pl @@ -0,0 +1,58 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 236 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-236 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Exact Change +Submitted by: Mohammad S Anwar + +You are asked to sell juice each costs $5. You are given an array of bills. You can only +sell ONE juice to each customer but make sure you return exact change back. You only have +$5, $10 and $20 notes. You do not have any change in hand at first. + +Write a script to find out if it is possible to sell to each customers with correct change. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; + +sub exactChange (@) { + my %r = (5 => 0, 10 => 0, 20 => 0); + + for (@_) { + if ($_ == 5) { + $r{5}++; + } elsif ($_ == 10) { + return 0 if ($r{5} < 1); + $r{5}--; $r{10}++; + } elsif ($_ == 20) { + my $v = $_; + if ($r{10} > 0) { + $r{10}--; $v -= 10; + } + my $n = ($v - 5) / 5; + return 0 if ($r{5} < $n); + $r{5} -= $n; $r{20}++; + } else { + return 0; + } + } + + return 1; +} + +is(exactChange(5,5,5,10,20),1); +is(exactChange(5,5,10,10,20),0); +is(exactChange(5,5,5,20),1); +is(exactChange(5,5,20),0); +is(exactChange(5,10,5,10,20),0); + +done_testing; diff --git a/challenge-236/perlboy1967/perl/ch2.pl b/challenge-236/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..0df3f42d16 --- /dev/null +++ b/challenge-236/perlboy1967/perl/ch2.pl @@ -0,0 +1,51 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 236 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-236 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Array Loops +Submitted by: Mark Anderson + +You are given an array of unique integers. + +Write a script to determine how many loops are in the given array. + +|| To determine a loop: Start at an index and take the number at array[index] +|| and then proceed to that index and continue this until you end up at the starting index. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; + +sub arrayLoops (@) { + my ($n,%u) = (0); + + for my $i (0 .. scalar(@_)-1) { + next if exists $u{$i}; + + my $j = $_[$i]; + while (!exists $u{$j}) { + last if ($j < 0 or $j >= scalar(@_)); + $j = $u{$j} = $_[$j]; + } + + $n++ if (exists $u{$j}); + } + return $n; +} + +is(arrayLoops(4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10),3); +is(arrayLoops(0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19),6); +is(arrayLoops(0,3,1,2),2); +is(arrayLoops(0,1,5,2),2); +is(arrayLoops(-1),0); + +done_testing; -- cgit From bf965c586e6486d39b0afdefc4b00a1052ffc484 Mon Sep 17 00:00:00 2001 From: deoac Date: Wed, 27 Sep 2023 20:59:26 -0400 Subject: Initial algorithm --- challenge-236/shimon-ben-avraham/raku/ch-2.raku | 70 +++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 challenge-236/shimon-ben-avraham/raku/ch-2.raku diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.raku b/challenge-236/shimon-ben-avraham/raku/ch-2.raku new file mode 100755 index 0000000000..6381c9fcf7 --- /dev/null +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.raku @@ -0,0 +1,70 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge #236 Task 2 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Wed 27 Sep 2023 08:58:34 PM EDT +# Version 0.0.1 + +# always use the latest version of Raku +use v6.*; + +subset UniqueIntArry of Array where *.Set.elems == .elems and .all ~~ Int; + +#| The actual program starts here. +multi MAIN (@ints) { + my $num-elems = @ints.elems; + my $index = 0; + my $start = 0; + my $next-index; + my $cur-value; + + my @cur-loop; + my @all-loops; + LOOP: + while $start < $num-elems { + $next-index = $cur-value = @ints[$index]; + @cur-loop.push: $cur-value; + @ints[$index] = Nil; + given $next-index { + when $start { + @all-loops.push: @cur-loop; + @cur-loop = (); + } + when $next-index ≥ $num-elems { + @all-loops.push: for @cur-loop; + } + } # end of given $next-index + $start = @ints.first: { $_.defined }; + last LOOP without $start; + } # end of while $index < $num-elems + say @all-loops.join("\n"); +} # end of multi MAIN ( ) + +#| Run with the option '--test' to test the program +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + ]; + + for @tests { +# cmp-ok ., ., ., .; + } # end of for @tests +} # end of multi MAIN (:$test!) + +my %*SUB-MAIN-OPTS = + :named-anywhere, # allow named variables at any location + :bundling, # allow bundling of named arguments +# :coerce-allomorphs-to(Str), # coerce allomorphic arguments to given type + :allow-no, # allow --no-foo as alternative to --/foo + :numeric-suffix-as-value, # allow -j2 as alternative to --j=2 +; + +#| Run with '--doc' to generate a document from the POD6 +#| It will be rendered in Text format +#| unless specified with the --format option. e.g. +#| --format=HTML +multi MAIN(Bool :$doc!, Str :$format = 'Text') { + run $*EXECUTABLE, "--doc=$format", $*PROGRAM; +} # end of multi MAIN(Bool :$man!) \ No newline at end of file -- cgit From 6496b947f2b9bd7f9df46aacec0ac7f859870a7b Mon Sep 17 00:00:00 2001 From: PerlMonk-Athanasius Date: Thu, 28 Sep 2023 15:56:26 +1000 Subject: Perl & Raku solutions to Tasks 1 & 2 for Week 236 --- challenge-236/athanasius/perl/ch-1.pl | 204 ++++++++++++++++++++++++++ challenge-236/athanasius/perl/ch-2.pl | 249 ++++++++++++++++++++++++++++++++ challenge-236/athanasius/raku/ch-1.raku | 197 +++++++++++++++++++++++++ challenge-236/athanasius/raku/ch-2.raku | 234 ++++++++++++++++++++++++++++++ 4 files changed, 884 insertions(+) create mode 100644 challenge-236/athanasius/perl/ch-1.pl create mode 100644 challenge-236/athanasius/perl/ch-2.pl create mode 100644 challenge-236/athanasius/raku/ch-1.raku create mode 100644 challenge-236/athanasius/raku/ch-2.raku diff --git a/challenge-236/athanasius/perl/ch-1.pl b/challenge-236/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..39c862f198 --- /dev/null +++ b/challenge-236/athanasius/perl/ch-1.pl @@ -0,0 +1,204 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 236 +========================= + +TASK #1 +------- +*Exact Change* + +Submitted by: Mohammad S Anwar + +You are asked to sell juice each costs $5. You are given an array of bills. You +can only sell ONE juice to each customer but make sure you return exact change +back. You only have $5, $10 and $20 notes. You do not have any change in hand at +first. + +Write a script to find out if it is possible to sell to each customers with +correct change. + +Example 1 + + Input: @bills = (5, 5, 5, 10, 20) + Output: true + + From the first 3 customers, we collect three $5 bills in order. + From the fourth customer, we collect a $10 bill and give back a $5. + From the fifth customer, we give a $10 bill and a $5 bill. + Since all customers got correct change, we output true. + +Example 2 + + Input: @bills = (5, 5, 10, 10, 20) + Output: false + + From the first two customers in order, we collect two $5 bills. + For the next two customers in order, we collect a $10 bill and give back a $5 + bill. + For the last customer, we can not give the change of $15 back because we only + have two $10 bills. + Since not every customer received the correct change, the answer is false. + +Example 3 + + Input: @bills = (5, 5, 5, 20) + Output: true + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2023 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=comment + +Interface +--------- +If no command-line arguments are given, the test suite is run. + +=cut +#=============================================================================== + +use v5.32.1; +use warnings; +use Const::Fast; +use Regexp::Common qw( number ); +use Test::More; + +const my $USAGE => +"Usage: + perl $0 [ ...] + perl $0 + + [ ...] A list of bills: \$5, \$10, and \$20 only\n"; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + $| = 1; + print "\nChallenge 236, Task #1: Exact Change (Perl)\n\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + if (scalar @ARGV == 0) + { + run_tests(); + } + else + { + my $bills = parse_command_line(); + + printf "Input: \@bills = (%s)\n", join ', ', @$bills; + + my $can_give_change = give_change( $bills ); + + printf "Output: %s\n", $can_give_change ? 'True' : 'False'; + } +} + +#------------------------------------------------------------------------------- +sub give_change +#------------------------------------------------------------------------------- +{ + my ($bills) = @_; + my %cash_in_hand = (5 => 0, 10 => 0, 20 => 0); + + for my $bill (@$bills) + { + ++$cash_in_hand{ $bill }; + + if ($bill == 10) # $5 change due + { + return 0 if $cash_in_hand{ 5 } == 0; + + --$cash_in_hand{ 5 }; + } + elsif ($bill == 20) # $15 change due + { + if ($cash_in_hand{ 10 } >= 1 && + $cash_in_hand{ 5 } >= 1) + { + --$cash_in_hand{ 10 }; + --$cash_in_hand{ 5 }; + } + elsif ($cash_in_hand{ 5 } >= 3) + { + $cash_in_hand{ 5 } -= 3; + } + else + { + return 0; + } + } + } + + return 1; +} + +#------------------------------------------------------------------------------- +sub parse_command_line +#------------------------------------------------------------------------------- +{ + for (@ARGV) + { + / ^ $RE{num}{int} $ /x + or error( qq["$_" is not a valid integer] ); + + $_ == 5 || $_ == 10 || $_ == 20 + or error( qq["$_" is not a valid note denomination]); + } + + return \@ARGV; +} + +#------------------------------------------------------------------------------- +sub run_tests +#------------------------------------------------------------------------------- +{ + print "Running the test suite\n"; + + while (my $line = ) + { + chomp $line; + + my ($test_name, $bills_str, $expected) = split / \| /x, $line; + + for ($test_name, $bills_str, $expected) + { + s/ ^ \s+ //x; + s/ \s+ $ //x; + } + + my @bills = split / \s+ /x, $bills_str; + my $can_give_change = give_change( \@bills ) ? 'True' : 'False'; + + is $can_give_change, $expected, $test_name; + } + + done_testing; +} + +#------------------------------------------------------------------------------- +sub error +#------------------------------------------------------------------------------- +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +################################################################################ + +__DATA__ +Example 1|5 5 5 10 20|True +Example 2|5 5 10 10 20|False +Example 3|5 5 5 20 |True diff --git a/challenge-236/athanasius/perl/ch-2.pl b/challenge-236/athanasius/perl/ch-2.pl new file mode 100644 index 0000000000..cc033fbbe3 --- /dev/null +++ b/challenge-236/athanasius/perl/ch-2.pl @@ -0,0 +1,249 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 236 +========================= + +TASK #2 +------- +*Array Loops* + +Submitted by: Mark Anderson + +You are given an array of unique integers. + +Write a script to determine how many loops are in the given array. + + To determine a loop: Start at an index and take the number at array[index] + and then proceed to that index and continue this until you end up at the + starting index. + +Example 1 + + Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) + Output: 3 + + To determine the 1st loop, start at index 0, the number at that index is 4, + proceed to index 4, the number at that index is 15, proceed to index 15 and so + on until you're back at index 0. + + Loops are as below: + [4 15 1 6 13 5 0] + [3 8 7 18 9 16 12 17 2] + [14 11 19 10] + +Example 2 + + Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) + Output: 6 + + Loops are as below: + [0] + [1] + [13 9 14 17 18 15 5 8 2] + [7 11 4 6 10 16 3] + [12] + [19] + +Example 3 + + Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) + Output: 1 + + Loop is as below: + [9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2023 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=comment + +Assumption +---------- +The contents of the input array are the valid indices for that array (in any +order). Therefore: +1. The minimum array value is 0. +2. For an array of n elements, the maximum array value is (n - 1). +3. Since the array values are unique, each valid index is included in the input + array exactly once. + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. Otherwise: +2. If VERBOSE is set to a true value (the default), the output is followed by + details of the loops found. + +=cut +#=============================================================================== + +use v5.32.1; +use warnings; +use Const::Fast; +use List::Util qw( max min uniqint ); +use Regexp::Common qw( number ); +use Test::More; + +const my $FALSE => 0; +const my $TRUE => 1; +const my $VERBOSE => $TRUE; +const my $USAGE => +"Usage: + perl $0 [ ...] + perl $0 + + [ ...] A non-empty list of all valid array indices in any order\n"; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + $| = 1; + print "\nChallenge 236, Task #2: Array Loops (Perl)\n\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + if (scalar @ARGV == 0) + { + run_tests(); + } + else + { + my $ints = parse_command_line(); + + printf "Input: \@ints = (%s)\n", join ',', @$ints; + + my $loops = find_loops( $ints ); + my $count = scalar @$loops; + + print "Output: $count\n"; + + if ($VERBOSE) + { + printf "\nLoop%s as below:\n", $count == 1 ? ' is' : 's are'; + printf "[%s]\n", join ' ', @$_ for @$loops; + } + } +} + +#------------------------------------------------------------------------------- +sub find_loops +#------------------------------------------------------------------------------- +{ + my ($ints) = @_; + my @loops; + my @found = ($FALSE) x scalar @$ints; + + for my $i (0 .. $#$ints) + { + next if $found[ $i ]; + + my $start = $ints->[ $i ]; + my @loop = $start; + my $last = $start; + my $next = -1; + + $found[ $start ] = 1; + + while ($TRUE) + { + $next = $ints->[ $last ]; + + last if $next == $start; + + push @loop, $next; + + $found[ $next ] = $TRUE; + + $last = $next; + } + + push @loops, [ @loop ]; + } + + return \@loops; +} + +#------------------------------------------------------------------------------- +sub parse_command_line +#------------------------------------------------------------------------------- +{ + / ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] ) + for @ARGV; + + my $min = min @ARGV; + my $max = max @ARGV; + + $min == 0 or error( qq[Minimum is "$min", should be 0] ); + $max == $#ARGV or error( qq[Maximum is "$max", should be $#ARGV] ); + + scalar @ARGV == scalar uniqint @ARGV + or error( qq[Duplicate found] ); + + return \@ARGV; +} + +#------------------------------------------------------------------------------- +sub run_tests +#------------------------------------------------------------------------------- +{ + print "Running the test suite\n"; + + while (my $line = ) + { + chomp $line; + + my ($test_name, $ints_str) = split / \| /x, $line; + + $line = ; + + my @exp_strs = split / \| /x, $line; + + for ($test_name, $ints_str, @exp_strs) + { + s/ ^ \s+ //x; + s/ \s+ $ //x; + } + + my @ints = split / \s+ /x, $ints_str; + my $loops = find_loops( \@ints ); + my @expected; + + for my $exp_str (@exp_strs) + { + push @expected, [ split / \s+ /x, $exp_str ]; + } + + is_deeply $loops, \@expected, $test_name; + } + + done_testing; +} + +#------------------------------------------------------------------------------- +sub error +#------------------------------------------------------------------------------- +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +################################################################################ + +__DATA__ +Example 1|4 6 3 8 15 0 13 18 7 16 14 19 17 5 11 1 12 2 9 10 + 4 15 1 6 13 5 0| 3 8 7 18 9 16 12 17 2|14 11 19 10 +Example 2|0 1 13 7 6 8 10 11 2 14 16 4 12 9 17 5 3 18 15 19 + 0| 1|13 9 14 17 18 15 5 8 2| 7 11 4 6 10 16 3|12|19 +Example 3|9 8 3 11 5 7 13 19 12 4 14 10 18 2 16 1 0 15 6 17 + 9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0 diff --git a/challenge-236/athanasius/raku/ch-1.raku b/challenge-236/athanasius/raku/ch-1.raku new file mode 100644 index 0000000000..1a5a2365ac --- /dev/null +++ b/challenge-236/athanasius/raku/ch-1.raku @@ -0,0 +1,197 @@ +use v6d; + +################################################################################ +=begin comment + +Perl Weekly Challenge 236 +========================= + +TASK #1 +------- +*Exact Change* + +Submitted by: Mohammad S Anwar + +You are asked to sell juice each costs $5. You are given an array of bills. You +can only sell ONE juice to each customer but make sure you return exact change +back. You only have $5, $10 and $20 notes. You do not have any change in hand at +first. + +Write a script to find out if it is possible to sell to each customers with +correct change. + +Example 1 + + Input: @bills = (5, 5, 5, 10, 20) + Output: true + + From the first 3 customers, we collect three $5 bills in order. + From the fourth customer, we collect a $10 bill and give back a $5. + From the fifth customer, we give a $10 bill and a $5 bill. + Since all customers got correct change, we output true. + +Example 2 + + Input: @bills = (5, 5, 10, 10, 20) + Output: false + + From the first two customers in order, we collect two $5 bills. + For the next two customers in order, we collect a $10 bill and give back a $5 + bill. + For the last customer, we can not give the change of $15 back because we only + have two $10 bills. + Since not every customer received the correct change, the answer is false. + +Example 3 + + Input: @bills = (5, 5, 5, 20) + Output: true + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2023 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=begin comment + +Interface +--------- +If no command-line arguments are given, the test suite is run. + +=end comment +#=============================================================================== + +use Test; + +subset Bill of Int where * ~~ 5 | 10 | 20; + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + "\nChallenge 236, Task #1: Exact Change (Raku)\n".put; +} + +#=============================================================================== +multi sub MAIN +( + #| A list of bills: $5, $10, and $20 only + + *@bills where { .elems > 0 && .all ~~ Bill:D } +) +#=============================================================================== +{ + "Input: \@bills = (%s)\n".printf: @bills.join: ', '; + + my Bool $can-give-change = give-change( @bills ); + + "Output: %s\n".printf: $can-give-change ?? 'True' !! 'False'; +} + +#=============================================================================== +multi sub MAIN() # No input: run the test suite +#=============================================================================== +{ + run-tests(); +} + +#------------------------------------------------------------------------------- +sub give-change( List:D[Bill:D] $bills --> Bool:D ) +#------------------------------------------------------------------------------- +{ + my UInt %cash-in-hand{Bill} = 5 => 0, 10 => 0, 20 => 0; + + for @$bills -> Bill $bill + { + ++%cash-in-hand{ $bill.Int }; + + if $bill == 10 # $5 change due + { + return False if %cash-in-hand{ 5 } == 0; + + --%ca