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 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 b126124c8de2687e8225cae7f8aaa393a2639165 Mon Sep 17 00:00:00 2001 From: deoac Date: Thu, 28 Sep 2023 20:33:20 -0400 Subject: Working! Now needs commenting... --- challenge-236/shimon-ben-avraham/raku/ch-2.md | 166 +++++++++++------------- challenge-236/shimon-ben-avraham/raku/ch-2.raku | 84 ++++++++---- challenge-236/shimon-ben-avraham/raku/ch-2.sl | 91 +++++++++++-- 3 files changed, 211 insertions(+), 130 deletions(-) diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.md b/challenge-236/shimon-ben-avraham/raku/ch-2.md index 8625d6862c..c792b50a66 100644 --- a/challenge-236/shimon-ben-avraham/raku/ch-2.md +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.md @@ -2,28 +2,19 @@ > ## 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) +[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: Mark Anderson -# The Challenge You are given an array of unique integers. +# 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.** @@ -74,61 +65,58 @@ Loop is as below: ``` - 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) + 1| subset UniqueIntArray of Array where .elems == 0 || + 2| .unique.elems == .elems and .all ~~ IntStr; + 3| + 4| multi MAIN (*@input where .all ~~ Int && + 5| .unique.elems == .elems, + 6| ) { + 7| my Int @ints = @input>>.Int; + 8| my Int $num-elems = @ints.elems; + 9| my Int $start-index = 0; + 10| my Int $cur-index = $start-index; + 11| + 12| my UniqueIntArray $cur-loop; + 13| my UniqueIntArray @all-loops; + 14| + 15| LOOP: + 16| while $start-index.defined { + 17| my $cur-value = @ints[$cur-index]; + 18| my $next-index = $cur-value; + 19| + 20| $cur-loop.push: $cur-value; + 21| @ints[$cur-index] = Nil; + 22| + 23| + 24| given $next-index { + 25| + 26| when * ≥ $num-elems { + 27| @all-loops.push: for $cur-loop; + 28| } + 29| + 30| when $start-index { + 31| @all-loops.push: $cur-loop; + 32| } + 33| + 34| default { + 35| $cur-index = $cur-value; + 36| next LOOP; + 37| } + 38| } + 39| + 40| $cur-loop = []; + 41| $start-index = $cur-index = @ints.first(*.defined, :k); + 42| + 43| } + 44| + 45| + 46| say @all-loops.elems; + 47| } -# 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) @@ -147,35 +135,27 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY ``` - 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| } + 48| multi MAIN (Bool :$test!) { + 49| use Test; + 50| + 51| my @tests = [ + 52| %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + 53| ]; + 54| + 55| for @tests { + 56| } + 57| } + 58| + 59| my %*SUB-MAIN-OPTS = + 60| :named-anywhere, + 61| :bundling, + 62| :allow-no, + 63| :numeric-suffix-as-value, + 64| ; + 65| + 66| multi MAIN(Bool :$doc!, Str :$format = 'Text') { + 67| run $*EXECUTABLE, "--doc=$format", $*PROGRAM; + 68| } ``` @@ -185,4 +165,4 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY ---- -Rendered from at 2023-09-25T20:20:54Z +Rendered from at 2023-09-29T00:30:04Z diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.raku b/challenge-236/shimon-ben-avraham/raku/ch-2.raku index 6381c9fcf7..4e93fa87fb 100755 --- a/challenge-236/shimon-ben-avraham/raku/ch-2.raku +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.raku @@ -2,42 +2,78 @@ # Perl Weekly Challenge #236 Task 2 # © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Wed 27 Sep 2023 08:58:34 PM EDT +# Last modified: Thu 28 Sep 2023 08:29:39 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; +subset UniqueIntArray of Array where .elems == 0 || + .unique.elems == .elems and .all ~~ IntStr; #| 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; +multi MAIN (*@input where .all ~~ Int && + .unique.elems == .elems, + Bool :v($verbose) = False + ) { + my Int @ints = @input>>.Int; + my Int $num-elems = @ints.elems; + my Int $start-index = 0; + my Int $cur-index = $start-index; + + my UniqueIntArray $cur-loop; + my UniqueIntArray @all-loops; + LOOP: - while $start < $num-elems { - $next-index = $cur-value = @ints[$index]; - @cur-loop.push: $cur-value; - @ints[$index] = Nil; + while $start-index.defined { + my $cur-value = @ints[$cur-index]; + my $next-index = $cur-value; + + $cur-loop.push: $cur-value; + @ints[$cur-index] = Nil; + + if $verbose { + dd @ints; + dd $start-index; + dd $cur-index; + dd $next-index; + dd $cur-value; + dd $cur-loop; + } # end of if $verbose + given $next-index { - when $start { - @all-loops.push: @cur-loop; - @cur-loop = (); + + when * ≥ $num-elems { + say "\e[31mFound singular loop[s]:\e[0m ", + $cur-loop.map({"[$_]"}).join(' ') if $verbose; + @all-loops.push: for $cur-loop; } - when $next-index ≥ $num-elems { - @all-loops.push: for @cur-loop; + + when $start-index { + say "\e[32mFound a loop:\e[0m ", + $cur-loop.join(" ") if $verbose; + @all-loops.push: $cur-loop; + } + + default { + say "\e[33mContinuing loop:\e[0m ", + $cur-loop.join(" ") if $verbose; + $cur-index = $cur-value; + next 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"); + + $cur-loop = []; + $start-index = $cur-index = @ints.first(*.defined, :k); + + say "\e[34m\nStarting new loop at index $start-index\e[0m" + if $start-index.defined && $verbose; + } # end of while $start-index.defined + + say "\n\n\e[35mAll loops:\n" ~ @all-loops.join("\n") ~ "\e[0m\n" + if $verbose; + + say @all-loops.elems; } # end of multi MAIN ( ) #| Run with the option '--test' to test the program diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.sl b/challenge-236/shimon-ben-avraham/raku/ch-2.sl index 3c8a4ad460..f750194fbb 100755 --- a/challenge-236/shimon-ben-avraham/raku/ch-2.sl +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.sl @@ -2,7 +2,7 @@ # Perl Weekly Challenge #236 Task 2 # © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Mon 25 Sep 2023 04:22:22 PM EDT +# Last modified: Thu 28 Sep 2023 08:29:39 PM EDT # Version 0.0.1 # begin-no-weave @@ -70,10 +70,84 @@ Loop is as below: =head1 The Solution =end pod +subset UniqueIntArray of Array where .elems == 0 || + .unique.elems == .elems and .all ~~ IntStr; #| The actual program starts here. -multi MAIN ( ) { - ; +multi MAIN (*@input where .all ~~ Int && + .unique.elems == .elems, + Bool :v($verbose) = False # no-weave-this-line + ) { + my Int @ints = @input>>.Int; + my Int $num-elems = @ints.elems; + my Int $start-index = 0; + my Int $cur-index = $start-index; + + my UniqueIntArray $cur-loop; + my UniqueIntArray @all-loops; + + LOOP: + while $start-index.defined { + my $cur-value = @ints[$cur-index]; + my $next-index = $cur-value; + + $cur-loop.push: $cur-value; + @ints[$cur-index] = Nil; + + #begin-no-weave + if $verbose { + dd @ints; + dd $start-index; + dd $cur-index; + dd $next-index; + dd $cur-value; + dd $cur-loop; + } # end of if $verbose + #end-no-weave + + given $next-index { + + when * ≥ $num-elems { + #begin-no-weave + say "\e[31mFound singular loop[s]:\e[0m ", + $cur-loop.map({"[$_]"}).join(' ') if $verbose; + #end-no-weave + @all-loops.push: for $cur-loop; + } + + when $start-index { + # begin-no-weave + say "\e[32mFound a loop:\e[0m ", + $cur-loop.join(" ") if $verbose; + # end-no-weave + @all-loops.push: $cur-loop; + } + + default { + #begin-no-weave + say "\e[33mContinuing loop:\e[0m ", + $cur-loop.join(" ") if $verbose; + #end-no-weave + $cur-index = $cur-value; + next LOOP; + } + } # end of given $next-index + + $cur-loop = []; + $start-index = $cur-index = @ints.first(*.defined, :k); + + # begin-no-weave + say "\e[34m\nStarting new loop at index $start-index\e[0m" + if $start-index.defined && $verbose; + # end-no-weave + } # end of while $start-index.defined + + #begin-no-weave + say "\n\n\e[35mAll loops:\n" ~ @all-loops.join("\n") ~ "\e[0m\n" + if $verbose; + #end-no-weave + + say @all-loops.elems; } # end of multi MAIN ( ) @@ -101,7 +175,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. =end pod #| Run with the option '--test' to test the program -multi MAIN (:$test!) { +multi MAIN (Bool :$test!) { use Test; my @tests = [ @@ -121,15 +195,6 @@ my %*SUB-MAIN-OPTS = :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. -- cgit From 78d00e80692e991c6f2d9f55479b9a9eb051bb6c Mon Sep 17 00:00:00 2001 From: deoac Date: Thu, 28 Sep 2023 22:28:33 -0400 Subject: 1) Added multi MAINs for bad input. 2) Added the three examples as tests. 3) Added Pod6 framework --- challenge-236/shimon-ben-avraham/raku/ch-2.md | 263 ++++++++++++++++-------- challenge-236/shimon-ben-avraham/raku/ch-2.raku | 60 +++--- challenge-236/shimon-ben-avraham/raku/ch-2.sl | 126 +++++++++--- 3 files changed, 313 insertions(+), 136 deletions(-) diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.md b/challenge-236/shimon-ben-avraham/raku/ch-2.md index c792b50a66..5fba7db498 100644 --- a/challenge-236/shimon-ben-avraham/raku/ch-2.md +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.md @@ -1,8 +1,9 @@ -# Challenge # 236 Task 2, Array Loops -> +# The Perl Weekly Challenge +>Submitted By: Mark Anderson + + ## Table of Contents -[Submitted By: Mark Anderson](#submitted-by-mark-anderson) -[The Challenge](#the-challenge) +[Challenge #236 Task 2, Array Loops](#challenge-236-task-2-array-loops) [Example 1](#example-1) [Example 2](#example-2) [Example 3](#example-3) @@ -11,8 +12,7 @@ [LICENCE AND COPYRIGHT](#licence-and-copyright) ---- -# Submitted By: Mark Anderson -# The Challenge +## Challenge #236 Task 2, Array Loops You are given an array of unique integers. Write a script to determine how many loops are in the given array. @@ -20,7 +20,7 @@ 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 +### 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 @@ -36,7 +36,7 @@ Loops are as below: ``` -## Example 2 +### 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 @@ -50,7 +50,7 @@ Loops are as below: [19] ``` -## Example 3 +### 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 @@ -59,7 +59,11 @@ 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 +## The Solution + + + + @@ -67,97 +71,194 @@ Loop is as below: ``` 1| subset UniqueIntArray of Array where .elems == 0 || 2| .unique.elems == .elems and .all ~~ IntStr; - 3| - 4| multi MAIN (*@input where .all ~~ Int && + +``` + + + + + + + + +``` + 3| multi MAIN (#| A list of unique integers + 4| *@input where .all ~~ Int && 5| .unique.elems == .elems, - 6| ) { - 7| my Int @ints = @input>>.Int; - 8| my Int $num-elems = @ints.elems; - 9| my Int $start-index = 0; - 10| my Int $cur-index = $start-index; - 11| + 6| + 7| ) { + +``` + + + + + + + + +``` + 8| my Int @ints = @input>>.Int; + 9| my Int $num-elems = @ints.elems; + 10| my Int $start-index = 0; + 11| my Int $cur-index = $start-index; + +``` + + + + + + + + +``` 12| my UniqueIntArray $cur-loop; 13| my UniqueIntArray @all-loops; - 14| - 15| LOOP: - 16| while $start-index.defined { - 17| my $cur-value = @ints[$cur-index]; - 18| my $next-index = $cur-value; - 19| - 20| $cur-loop.push: $cur-value; - 21| @ints[$cur-index] = Nil; - 22| - 23| - 24| given $next-index { - 25| - 26| when * ≥ $num-elems { - 27| @all-loops.push: for $cur-loop; - 28| } - 29| - 30| when $start-index { - 31| @all-loops.push: $cur-loop; - 32| } - 33| - 34| default { - 35| $cur-index = $cur-value; - 36| next LOOP; - 37| } - 38| } - 39| - 40| $cur-loop = []; - 41| $start-index = $cur-index = @ints.first(*.defined, :k); - 42| - 43| } - 44| - 45| - 46| say @all-loops.elems; - 47| } ``` -# 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. +``` + 14| LOOP: + 15| while $start-index.defined { + +``` + + + + + + + + +``` + 16| my $cur-value = @ints[$cur-index]; + 17| my $next-index = $cur-value; + +``` + + + + +``` + 18| $cur-loop.push: $cur-value; + 19| @ints[$cur-index] = Nil; + 20| ``` - 48| multi MAIN (Bool :$test!) { - 49| use Test; - 50| - 51| my @tests = [ - 52| %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, - 53| ]; - 54| - 55| for @tests { - 56| } - 57| } - 58| - 59| my %*SUB-MAIN-OPTS = - 60| :named-anywhere, - 61| :bundling, - 62| :allow-no, - 63| :numeric-suffix-as-value, - 64| ; - 65| - 66| multi MAIN(Bool :$doc!, Str :$format = 'Text') { - 67| run $*EXECUTABLE, "--doc=$format", $*PROGRAM; - 68| } + + + + + + + ``` + 21| given $next-index { + +``` + + + + + + + + +``` + 22| when * ≥ $num-elems { + 23| @all-loops.push: for $cur-loop; + 24| } + +``` + + + + + + + + +``` + 25| when $start-index { + 26| @all-loops.push: $cur-loop; + 27| } + +``` + + + + + + + + +``` + 28| default { + 29| $cur-index = $cur-value; + 30| next LOOP; + 31| } + 32| } + +``` + + + + + + + + +``` + 33| $cur-loop = []; + 34| $start-index = $cur-index = @ints.first(*.defined, :k); + 35| + 36| } + 37| + +``` + + + + + + + + +``` + 38| say @all-loops.elems; + 39| return @all-loops.elems; + 40| } + +``` + + + + +# AUTHOR +Shimon Bollinger (deoac.shimon@gmail.com) + +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. + @@ -165,4 +266,4 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY ---- -Rendered from at 2023-09-29T00:30:04Z +Rendered from at 2023-09-29T02:25:06Z diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.raku b/challenge-236/shimon-ben-avraham/raku/ch-2.raku index 4e93fa87fb..2db08b709c 100755 --- a/challenge-236/shimon-ben-avraham/raku/ch-2.raku +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.raku @@ -2,20 +2,24 @@ # Perl Weekly Challenge #236 Task 2 # © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Thu 28 Sep 2023 08:29:39 PM EDT +# Last modified: Thu 28 Sep 2023 10:26:18 PM EDT # Version 0.0.1 # always use the latest version of Raku use v6.*; + subset UniqueIntArray of Array where .elems == 0 || .unique.elems == .elems and .all ~~ IntStr; -#| The actual program starts here. -multi MAIN (*@input where .all ~~ Int && +multi MAIN (#| A list of unique integers + *@input where .all ~~ Int && .unique.elems == .elems, - Bool :v($verbose) = False + + #| Show debug prints when True + Bool :v($verbose) = False ) { + my Int @ints = @input>>.Int; my Int $num-elems = @ints.elems; my Int $start-index = 0; @@ -26,6 +30,7 @@ multi MAIN (*@input where .all ~~ Int && LOOP: while $start-index.defined { + my $cur-value = @ints[$cur-index]; my $next-index = $cur-value; @@ -74,33 +79,42 @@ multi MAIN (*@input where .all ~~ Int && if $verbose; say @all-loops.elems; + return @all-loops.elems; } # end of multi MAIN ( ) -#| Run with the option '--test' to test the program +# Catch invalid input +multi MAIN (*@input where .all !~~ Int) { + note "Input must be a list of *integers*"; + exit 1; +} # end of multi MAIN (*@input where .all !~~ Int) + +multi MAIN (*@input where .unique.elems != .elems) { + note "Input must be a list of *unique* integers"; + exit 1; +} # end of multi MAIN (*@input where .unique.elems != .elems) + +multi MAIN () { + note "Input cannot be empty"; + exit 1; +} # end of multi MAIN () + +#| Run with the option '--test' to, well, run the tests! multi MAIN (Bool :$test!) { use Test; + #TODO Handle edge cases. + #TODO Test 1 integer array my @tests = [ - %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + %{ got => MAIN(4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10), + op => '==', expected => 3, desc => 'Example 1' }, + %{ got => MAIN(0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19), + op => '==', expected => 6, desc => 'Example 2' }, + %{ got => MAIN(9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17), + op => '==', expected => 1, desc => 'Example 3' }, ]; + plan +@tests; for @tests { -# cmp-ok ., ., ., .; + 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 diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.sl b/challenge-236/shimon-ben-avraham/raku/ch-2.sl index f750194fbb..41147e6efa 100755 --- a/challenge-236/shimon-ben-avraham/raku/ch-2.sl +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.sl @@ -2,7 +2,7 @@ # Perl Weekly Challenge #236 Task 2 # © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Thu 28 Sep 2023 08:29:39 PM EDT +# Last modified: Thu 28 Sep 2023 10:27:08 PM EDT # Version 0.0.1 # begin-no-weave @@ -11,11 +11,11 @@ use v6.*; # end-no-weave =begin pod -=TITLE Challenge # 236 Task 2, Array Loops +=TITLE The Perl Weekly Challenge -=head1 Submitted By: Mark Anderson +=SUBTITLE Submitted By: Mark Anderson -=head1 The Challenge +=head2 Challenge #236 Task 2, Array Loops You are given an array of unique integers. @@ -24,7 +24,7 @@ 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 +=head3 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) @@ -41,7 +41,7 @@ Loops are as below: =end code -=head2 Example 2 +=head3 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) @@ -56,7 +56,7 @@ Loops are as below: [19] =end code -=head2 Example 3 +=head3 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) @@ -67,30 +67,59 @@ Loop is as below: =end code -=head1 The Solution +=head2 The Solution +=end pod + +=begin pod =end pod subset UniqueIntArray of Array where .elems == 0 || .unique.elems == .elems and .all ~~ IntStr; +=begin pod + +=end pod + -#| The actual program starts here. -multi MAIN (*@input where .all ~~ Int && +multi MAIN (#| A list of unique integers + *@input where .all ~~ Int && .unique.elems == .elems, + + #| Show debug prints when True Bool :v($verbose) = False # no-weave-this-line ) { +=begin pod + +=end pod + my Int @ints = @input>>.Int; my Int $num-elems = @ints.elems; my Int $start-index = 0; my Int $cur-index = $start-index; +=begin pod + +=end pod + my UniqueIntArray $cur-loop; my UniqueIntArray @all-loops; +=begin pod + +=end pod + LOOP: while $start-index.defined { +=begin pod + +=end pod + my $cur-value = @ints[$cur-index]; my $next-index = $cur-value; +=begin pod + +=end pod + $cur-loop.push: $cur-value; @ints[$cur-index] = Nil; @@ -105,7 +134,14 @@ multi MAIN (*@input where .all ~~ Int && } # end of if $verbose #end-no-weave +=begin pod + +=end pod + given $next-index { +=begin pod + +=end pod when * ≥ $num-elems { #begin-no-weave @@ -114,6 +150,9 @@ multi MAIN (*@input where .all ~~ Int && #end-no-weave @all-loops.push: for $cur-loop; } +=begin pod + +=end pod when $start-index { # begin-no-weave @@ -123,6 +162,10 @@ multi MAIN (*@input where .all ~~ Int && @all-loops.push: $cur-loop; } +=begin pod + +=end pod + default { #begin-no-weave say "\e[33mContinuing loop:\e[0m ", @@ -132,6 +175,9 @@ multi MAIN (*@input where .all ~~ Int && next LOOP; } } # end of given $next-index +=begin pod + +=end pod $cur-loop = []; $start-index = $cur-index = @ints.first(*.defined, :k); @@ -146,8 +192,13 @@ multi MAIN (*@input where .all ~~ Int && say "\n\n\e[35mAll loops:\n" ~ @all-loops.join("\n") ~ "\e[0m\n" if $verbose; #end-no-weave +=begin pod + +=end pod + say @all-loops.elems; + return @all-loops.elems; } # end of multi MAIN ( ) @@ -157,8 +208,10 @@ multi MAIN (*@input where .all ~~ Int && Shimon Bollinger (deoac.shimon@gmail.com) -Source can be located at: https://github.com/deoac/... . Comments and -Pull Requests are welcome. +=comment Source can be located at: +Z + +Comments and Pull Requests are welcome. =head1 LICENCE AND COPYRIGHT @@ -174,33 +227,42 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. =end pod -#| Run with the option '--test' to test the program +# begin-no-weave +# multi MAINs to catch invalid input +multi MAIN (*@input where .all !~~ Int) { + note "Input must be a list of *integers*"; + exit 1; +} # end of multi MAIN (*@input where .all !~~ Int) + +multi MAIN (*@input where .unique.elems != .elems) { + note "Input must be a list of *unique* integers"; + exit 1; +} # end of multi MAIN (*@input where .unique.elems != .elems) + +multi MAIN () { + note "Input cannot be empty"; + exit 1; +} # end of multi MAIN () + +#| Run with the option '--test' to run the program with the three examples. multi MAIN (Bool :$test!) { use Test; + #TODO Handle edge cases. + #TODO Test 1 integer array my @tests = [ - %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + %{ got => MAIN(4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10), + op => '==', expected => 3, desc => 'Example 1' }, + %{ got => MAIN(0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19), + op => '==', expected => 6, desc => 'Example 2' }, + %{ got => MAIN(9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17), + op => '==', expected => 1, desc => 'Example 3' }, ]; + plan +@tests; for @tests { -# cmp-ok ., ., ., .; + 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!) - +# end-no-weave -- cgit From 0bf3f4aa70e8500389deb0982f958e9f8b9a6fdf Mon Sep 17 00:00:00 2001 From: deoac Date: Fri, 29 Sep 2023 19:37:18 -0400 Subject: Can handle the case of an array of length 1 --- challenge-236/shimon-ben-avraham/raku/ch-2.sl | 37 ++++++++++++++++++--------- 1 file changed, 25 insertions(+), 12 deletions(-) mode change 100755 => 100644 challenge-236/shimon-ben-avraham/raku/ch-2.sl diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.sl b/challenge-236/shimon-ben-avraham/raku/ch-2.sl old mode 100755 new mode 100644 index 41147e6efa..bab91cbdf1 --- a/challenge-236/shimon-ben-avraham/raku/ch-2.sl +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.sl @@ -2,7 +2,7 @@ # Perl Weekly Challenge #236 Task 2 # © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Thu 28 Sep 2023 10:27:08 PM EDT +# Last modified: Fri 29 Sep 2023 07:35:45 PM EDT # Version 0.0.1 # begin-no-weave @@ -68,13 +68,10 @@ Loop is as below: =head2 The Solution -=end pod -=begin pod + =end pod -subset UniqueIntArray of Array where .elems == 0 || - .unique.elems == .elems and .all ~~ IntStr; =begin pod =end pod @@ -97,13 +94,21 @@ multi MAIN (#| A list of unique integers my Int $cur-index = $start-index; =begin pod +For type safety, we will create a subset to represent an array of unique +integers. =end pod - my UniqueIntArray $cur-loop; - my UniqueIntArray @all-loops; +subset UniqueIntArray of Array where .elems == 0 or + .unique.elems == .elems and + .all ~~ IntStr; + + my UniqueIntArray $cur-loop = []; + my UniqueIntArray @all-loops = []; =begin pod +We will create a pointer to the first index of the array and attempt to find +a loop that starts with that element. If we find a loop, we will push it to an array of loops. If we =end pod @@ -209,7 +214,7 @@ multi MAIN (#| A list of unique integers Shimon Bollinger (deoac.shimon@gmail.com) =comment Source can be located at: -Z +Z Comments and Pull Requests are welcome. @@ -229,22 +234,30 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # begin-no-weave # multi MAINs to catch invalid input -multi MAIN (*@input where .all !~~ Int) { + +# The weird matching syntax is because !~~ does not play well with Junctions. +multi MAIN (*@input where !(*.all ~~ Int)) is hidden-from-USAGE { note "Input must be a list of *integers*"; exit 1; } # end of multi MAIN (*@input where .all !~~ Int) -multi MAIN (*@input where .unique.elems != .elems) { +multi MAIN (*@input where .unique.elems != .elems) is hidden-from-USAGE { note "Input must be a list of *unique* integers"; exit 1; } # end of multi MAIN (*@input where .unique.elems != .elems) -multi MAIN () { +multi MAIN () is hidden-from-USAGE { note "Input cannot be empty"; exit 1; } # end of multi MAIN () -#| Run with the option '--test' to run the program with the three examples. +#| Handle the case of a single integer array +multi MAIN (Int $i!, Bool :v(:$verbose) = False) is hidden-from-USAGE { + note "\e[31mFound a singular loop:\e[0m [$i]" if $verbose; + say 1; +} # end of multi MAIN (Int $i! + +#| Use the option '--test' to run the program with the three examples. multi MAIN (Bool :$test!) { use Test; -- cgit From a0759246c24c2b2dc66c75a7b44d3acf5ff0e076 Mon Sep 17 00:00:00 2001 From: deoac Date: Fri, 29 Sep 2023 19:51:17 -0400 Subject: Singular lists handled correctly --- challenge-236/shimon-ben-avraham/raku/ch-2.raku | 32 ++++++++++++++----------- challenge-236/shimon-ben-avraham/raku/ch-2.sl | 13 ++++------ 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.raku b/challenge-236/shimon-ben-avraham/raku/ch-2.ra