diff options
| author | deoac <deoac.bollinger@gmail.com> | 2023-09-29 21:38:36 -0400 |
|---|---|---|
| committer | deoac <deoac.bollinger@gmail.com> | 2023-09-29 21:38:36 -0400 |
| commit | 8715c048dcf867ffb15fdfd2f0129527df07c990 (patch) | |
| tree | 8eaca578605a5907115bea31fb1cc19cfee13385 | |
| parent | 54e704e0f0b6d00705cae1a64fd6bde42a1c6d37 (diff) | |
| download | perlweeklychallenge-club-8715c048dcf867ffb15fdfd2f0129527df07c990.tar.gz perlweeklychallenge-club-8715c048dcf867ffb15fdfd2f0129527df07c990.tar.bz2 perlweeklychallenge-club-8715c048dcf867ffb15fdfd2f0129527df07c990.zip | |
1) Needed to clone @cur-loop. 2) Added samplbe verbose output. 3) Finessed the .md file
| -rw-r--r-- | challenge-236/shimon-ben-avraham/raku/ch-2.md | 138 | ||||
| -rwxr-xr-x | challenge-236/shimon-ben-avraham/raku/ch-2.raku | 14 | ||||
| -rw-r--r-- | challenge-236/shimon-ben-avraham/raku/ch-2.sl | 98 |
3 files changed, 205 insertions, 45 deletions
diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.md b/challenge-236/shimon-ben-avraham/raku/ch-2.md index 17a7ad805d..cca2374700 100644 --- a/challenge-236/shimon-ben-avraham/raku/ch-2.md +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.md @@ -4,14 +4,15 @@ ## Table of Contents [Challenge #236 Task 2, Array Loops](#challenge-236-task-2-array-loops) -[Example 1](#example-1) -[Example 2](#example-2) -[Example 3](#example-3) + [Example 1](#example-1) + [Example 2](#example-2) + [Example 3](#example-3) [The Solution](#the-solution) -[The Basic Algorithm](#the-basic-algorithm) -[Initialize variables](#initialize-variables) -[The Main Loop](#the-main-loop) -[Print and return the number of loops found.](#print-and-return-the-number-of-loops-found) + [The Basic Algorithm](#the-basic-algorithm) + [Initialize variables](#initialize-variables) + [The Main Loop](#the-main-loop) + [Print and return the number of loops found.](#print-and-return-the-number-of-loops-found) +[Sample run with debug prints](#sample-run-with-debug-prints) [AUTHOR](#author) [LICENCE AND COPYRIGHT](#licence-and-copyright) @@ -192,7 +193,7 @@ At this point there are three possibilities: * We have reached an index that is greater than the number of elements in the original array. -This means we have found a loop that is not closed. Each element we've found so far is a loop by itself. So we push each element to the list of all loops. +Thus, we have found a loop that is not closed. Each element we've found so far is a loop by itself. So we push each element to the list of all loops. @@ -201,7 +202,7 @@ This means we have found a loop that is not closed. Each element we've found so ``` 19| when * ≥ $num-elems { 20| @all-loops.push: $_ for @cur-loop; - 21| } + 21| } ``` @@ -218,8 +219,8 @@ When the next index is the same as the start pointer, we have found a closed loo ``` 22| when $start-pointer { - 23| @all-loops.push: @cur-loop; - 24| } + 23| @all-loops.push: @cur-loop.clone; + 24| } ``` @@ -238,26 +239,27 @@ So we continue looking for the next element in the loop by updating the current 25| default { 26| $cur-index = $cur-value; 27| next INDEX; - 28| } - 29| } + 28| } + 29| + 30| } ``` -At this point we have found a loop or a singular loop. We need to find the next start pointer by looking for the next defined element in the array. +At this point we have found a loop or singular loop[s]. We need to find the next start pointer by looking for the next defined element in the array. ``` - 30| @cur-loop = []; - 31| $start-pointer = $cur-index = @ints.first(*.defined, :k); - 32| - 33| } - 34| + 31| @cur-loop = []; + 32| $start-pointer = $cur-index = @ints.first(*.defined, :k); + 33| + 34| } + 35| ``` @@ -270,33 +272,103 @@ At this point we have found a loop or a singular loop. We need to find the next ``` - 35| say @all-loops.elems; - 36| return @all-loops.elems; - 37| } + 36| say @all-loops.elems; + 37| + 38| return @all-loops.elems; + 39| } ``` -# AUTHOR -Shimon Bollinger (deoac.shimon@gmail.com) - -Comments and Pull Requests are welcome. +## Sample run with debug prints +(The option `--verbose` and the debug print statemnts are not shown in the above code.) -# LICENCE AND COPYRIGHT -© 2023 Shimon Bollinger. All rights reserved. +``` +./ch-2.raku --verbose 1 0 8 5 4 3 9 + +Array[Int] @ints = Array[Int].new(Int, 0, 8, 5, 4, 3, 9) +Int $start-pointer = 0 +Int $cur-index = 0 +Int $next-index = 1 +Int $cur-value = 1 +Array @cur-loop = [1] +Continuing loop: 1 +Array[Int] @ints = Array[Int].new(Int, Int, 8, 5, 4, 3, 9) +Int $start-pointer = 0 +Int $cur-index = 1 +Int $next-index = 0 +Int $cur-value = 0 +Array @cur-loop = [1, 0] +Found a loop: 1 0 + +Starting new loop at index 2 +Array[Int] @ints = Array[Int].new(Int, Int, Int, 5, 4, 3, 9) +Int $start-pointer = 2 +Int $cur-index = 2 +Int $next-index = 8 +Int $cur-value = 8 +Array @cur-loop = [8] +Found singular loop[s]: [8] + +Starting new loop at index 3 +Array[Int] @ints = Array[Int].new(Int, Int, Int, Int, 4, 3, 9) +Int $start-pointer = 3 +Int $cur-index = 3 +Int $next-index = 5 +Int $cur-value = 5 +Array @cur-loop = [5] +Continuing loop: 5 +Array[Int] @ints = Array[Int].new(Int, Int, Int, Int, 4, Int, 9) +Int $start-pointer = 3 +Int $cur-index = 5 +Int $next-index = 3 +Int $cur-value = 3 +Array @cur-loop = [5, 3] +Found a loop: 5 3 + +Starting new loop at index 4 +Array[Int] @ints = Array[Int].new(Int, Int, Int, Int, Int, Int, 9) +Int $start-pointer = 4 +Int $cur-index = 4 +Int $next-index = 4 +Int $cur-value = 4 +Array @cur-loop = [4] +Found a loop: 4 + +Starting new loop at index 6 +Array[Int] @ints = Array[Int].new(Int, Int, Int, Int, Int, Int, Int) +Int $start-pointer = 6 +Int $cur-index = 6 +Int $next-index = 9 +Int $cur-value = 9 +Array @cur-loop = [9] +Found singular loop[s]: [9] + + +All loops: +1 0 +8 +5 3 +4 +9 + +Number of loops: 5 -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. +# 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). ----- -Rendered from at 2023-09-30T00:53:05Z -
\ No newline at end of file +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.
\ No newline at end of file diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.raku b/challenge-236/shimon-ben-avraham/raku/ch-2.raku index 489b237a89..980ecb86d7 100755 --- a/challenge-236/shimon-ben-avraham/raku/ch-2.raku +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.raku @@ -2,7 +2,7 @@ # Perl Weekly Challenge #236 Task 2 # © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Fri 29 Sep 2023 08:48:19 PM EDT +# Last modified: Fri 29 Sep 2023 09:34:53 PM EDT # Version 0.0.1 # always use the latest version of Raku @@ -48,20 +48,21 @@ multi MAIN (#| A list of unique integers say "\e[31mFound singular loop[s]:\e[0m ", @cur-loop.map({"[$_]"}).join(' ') if $verbose; @all-loops.push: $_ for @cur-loop; - } + } # end of when * ≥ $num-elems when $start-pointer { say "\e[32mFound a loop:\e[0m ", @cur-loop.join(" ") if $verbose; - @all-loops.push: @cur-loop; - } + @all-loops.push: @cur-loop.clone; + } # end of when $start-pointer default { say "\e[33mContinuing loop:\e[0m ", @cur-loop.join(" ") if $verbose; $cur-index = $cur-value; next INDEX; - } + } # end of default + } # end of given $next-index @cur-loop = []; @@ -74,10 +75,13 @@ multi MAIN (#| A list of unique integers say "\n\n\e[35mAll loops:\n" ~ @all-loops.join("\n") ~ "\e[0m\n" if $verbose; + print "Number of loops: " if $verbose; say @all-loops.elems; + return @all-loops.elems; } # end of multi MAIN ( ) + # multi MAINs to catch invalid input # The weird matching syntax is because !~~ does not play well with Junctions. multi MAIN (Bool :$v = False, diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.sl b/challenge-236/shimon-ben-avraham/raku/ch-2.sl index 8f81d91c93..06c4b3adae 100644 --- 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: Fri 29 Sep 2023 08:53:49 PM EDT +# Last modified: Fri 29 Sep 2023 09:34:53 PM EDT # Version 0.0.1 # begin-no-weave @@ -175,7 +175,7 @@ At this point there are three possibilities: =item We have reached an index that is greater than the number of elements in the original array. -This means we have found a loop that is not closed. Each element we've +Thus, we have found a loop that is not closed. Each element we've found so far is a loop by itself. So we push each element to the list of all loops. @@ -187,7 +187,7 @@ all loops. @cur-loop.map({"[$_]"}).join(' ') if $verbose; #end-no-weave @all-loops.push: $_ for @cur-loop; - } + } # end of when * ≥ $num-elems =begin pod =item We have found a closed loop @@ -202,8 +202,8 @@ a closed loop. We push the current loop to the list of all loops. say "\e[32mFound a loop:\e[0m ", @cur-loop.join(" ") if $verbose; # end-no-weave - @all-loops.push: @cur-loop; - } + @all-loops.push: @cur-loop.clone; + } # end of when $start-pointer =begin pod =item We have found a value that is not in the current loop. @@ -220,7 +220,8 @@ index. #end-no-weave $cur-index = $cur-value; next INDEX; - } + } # end of default + } # end of given $next-index =begin pod @@ -248,11 +249,94 @@ next start pointer by looking for the next defined element in the array. =end pod - + print "Number of loops: " if $verbose; # no-weave-this-line say @all-loops.elems; + return @all-loops.elems; } # end of multi MAIN ( ) +=begin pod + +=head2 Sample run with debug prints + +(The option C<--verbose> and the debug print statemnts are not shown in the +above code.) + +=begin code :lang<sh> + +./ch-2.raku --verbose 1 0 8 5 4 3 9 + +Array[Int] @ints = Array[Int].new(Int, 0, 8, 5, 4, 3, 9) +Int $start-pointer = 0 +Int $cur-index = 0 +Int $next-index = 1 +Int $cur-value = 1 +Array @cur-loop = [1] +Continuing loop: 1 +Array[Int] @ints = Array[Int].new(Int, Int, 8, 5, 4, 3, 9) +Int $start-pointer = 0 +Int $cur-index = 1 +Int $next-index = 0 +Int $cur-value = 0 +Array @cur-loop = [1, 0] +Found a loop: 1 0 + +Starting new loop at index 2 +Array[Int] @ints = Array[Int].new(Int, Int, Int, 5, 4, 3, 9) +Int $start-pointer = 2 +Int $cur-index = 2 +Int $next-index = 8 +Int $cur-value = 8 +Array @cur-loop = [8] +Found singular loop[s]: [8] + +Starting new loop at index 3 +Array[Int] @ints = Array[Int].new(Int, Int, Int, Int, 4, 3, 9) +Int $start-pointer = 3 +Int $cur-index = 3 +Int $next-index = 5 +Int $cur-value = 5 +Array @cur-loop = [5] +Continuing loop: 5 +Array[Int] @ints = Array[Int].new(Int, Int, Int, Int, 4, Int, 9) +Int $start-pointer = 3 +Int $cur-index = 5 +Int $next-index = 3 +Int $cur-value = 3 +Array @cur-loop = [5, 3] +Found a loop: 5 3 + +Starting new loop at index 4 +Array[Int] @ints = Array[Int].new(Int, Int, Int, Int, Int, Int, 9) +Int $start-pointer = 4 +Int $cur-index = 4 +Int $next-index = 4 +Int $cur-value = 4 +Array @cur-loop = [4] +Found a loop: 4 + +Starting new loop at index 6 +Array[Int] @ints = Array[Int].new(Int, Int, Int, Int, Int, Int, Int) +Int $start-pointer = 6 +Int $cur-index = 6 +Int $next-index = 9 +Int $cur-value = 9 +Array @cur-loop = [9] +Found singular loop[s]: [9] + + +All loops: +1 0 +8 +5 3 +4 +9 + +Number of loops: 5 +=end code + +=end pod + =begin pod |
