diff options
| author | deoac <deoac.bollinger@gmail.com> | 2023-09-29 22:31:06 -0400 |
|---|---|---|
| committer | deoac <deoac.bollinger@gmail.com> | 2023-09-29 22:32:09 -0400 |
| commit | d171767647fd1a77e4f9877fb0ac63dde7551854 (patch) | |
| tree | 3395cfdedf3e4b31eecbcbbf11b239f07e5ad7f4 | |
| parent | 8715c048dcf867ffb15fdfd2f0129527df07c990 (diff) | |
| download | perlweeklychallenge-club-d171767647fd1a77e4f9877fb0ac63dde7551854.tar.gz perlweeklychallenge-club-d171767647fd1a77e4f9877fb0ac63dde7551854.tar.bz2 perlweeklychallenge-club-d171767647fd1a77e4f9877fb0ac63dde7551854.zip | |
Minor text editing
| -rw-r--r-- | challenge-236/shimon-ben-avraham/raku/ch-2.md | 82 | ||||
| -rwxr-xr-x | challenge-236/shimon-ben-avraham/raku/ch-2.raku | 13 | ||||
| -rw-r--r-- | challenge-236/shimon-ben-avraham/raku/ch-2.sl | 35 |
3 files changed, 62 insertions, 68 deletions
diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.md b/challenge-236/shimon-ben-avraham/raku/ch-2.md index cca2374700..876496dd41 100644 --- a/challenge-236/shimon-ben-avraham/raku/ch-2.md +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.md @@ -4,14 +4,14 @@ ## 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) @@ -74,11 +74,9 @@ We will create a pointer to the first index of the array and attempt to find a l It's important to remember that each element can be a part of only _one_ loop, even if it is a loop by itself. -Every time we find an element, we will push it to a loop array and set the element to `Nil` so that we don't use it again. +Every time we find an element, we will push it to a current-loop array and set the element to `Nil` so that we don't use it again. -If we find a loop, we will push it to an array of loops. If we don't find a loop, we will move the start pointer to the next defined element and try again. - -Note that a 'loop' is defined as a list of integers, not a list of indices. +If we find a loop[ 1 ], we will push it to an array of loops. Note that a loop can consist of a single element. After we find a loop, we will move the start pointer to the next defined element and try again. First we will only accept input that is a list of unique integers. @@ -115,8 +113,6 @@ First we will only accept input that is a list of unique integers. The current loop we are working on is stored in `@cur-loop`. The list of all found loops is stored in `@all-loops`. -Note that a loop can consist of a single element. - @@ -170,7 +166,6 @@ Each value we are looking at gets pushed to the current loop array and set to `N ``` 15| @cur-loop.push: $cur-value; 16| @ints[$cur-index] = Nil; - 17| ``` @@ -184,7 +179,7 @@ At this point there are three possibilities: ``` - 18| given $next-index { + 17| given $next-index { ``` @@ -200,9 +195,9 @@ Thus, we have found a loop that is not closed. Each element we've found so far i ``` - 19| when * ≥ $num-elems { - 20| @all-loops.push: $_ for @cur-loop; - 21| } + 18| when * ≥ $num-elems { + 19| @all-loops.push: $_ for @cur-loop; + 20| } ``` @@ -218,9 +213,9 @@ 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.clone; - 24| } + 21| when $start-pointer { + 22| @all-loops.push: @cur-loop.clone; + 23| } ``` @@ -236,30 +231,30 @@ 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| - 30| } + 24| default { + 25| $cur-index = $cur-value; + 26| next INDEX; + 27| } + 28| + 29| } ``` -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. +At this point we have found a loop or single-item loop[s]. We need to find the next start pointer by looking for the next defined element in the array. ``` - 31| @cur-loop = []; - 32| $start-pointer = $cur-index = @ints.first(*.defined, :k); - 33| - 34| } - 35| + 30| @cur-loop = []; + 31| $start-pointer = $cur-index = @ints.first(*.defined, :k); + 32| + 33| } + 34| ``` @@ -272,10 +267,10 @@ At this point we have found a loop or singular loop[s]. We need to find the next ``` - 36| say @all-loops.elems; - 37| - 38| return @all-loops.elems; - 39| } + 35| say @all-loops.elems; + 36| + 37| return @all-loops.elems; + 38| } ``` @@ -283,10 +278,10 @@ At this point we have found a loop or singular loop[s]. We need to find the next ## Sample run with debug prints -(The option `--verbose` and the debug print statemnts are not shown in the above code.) +(The option `--verbose` and the debug print statements are not shown in the above code.) ``` -./ch-2.raku --verbose 1 0 8 5 4 3 9 +$ ./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 @@ -310,7 +305,7 @@ Int $cur-index = 2 Int $next-index = 8 Int $cur-value = 8 Array @cur-loop = [8] -Found singular loop[s]: [8] +Found single-item loop[s]: [8] Starting new loop at index 3 Array[Int] @ints = Array[Int].new(Int, Int, Int, Int, 4, 3, 9) @@ -344,7 +339,7 @@ Int $cur-index = 6 Int $next-index = 9 Int $cur-value = 9 Array @cur-loop = [9] -Found singular loop[s]: [9] +Found single-item loop[s]: [9] All loops: @@ -371,4 +366,7 @@ Comments and Pull Requests are welcome. 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.
\ 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. + +---- +###### 1 A 'loop' is defined as a list of array element values, not a list of the array's indices. diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.raku b/challenge-236/shimon-ben-avraham/raku/ch-2.raku index 980ecb86d7..8d57868fd0 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 09:34:53 PM EDT +# Last modified: Fri 29 Sep 2023 10:31:25 PM EDT # Version 0.0.1 # always use the latest version of Raku @@ -13,7 +13,7 @@ multi MAIN (#| A list of unique integers *@input where .all ~~ Int && .unique.elems == .elems, #| Show debug prints when True - Bool :v($verbose) = False + Bool :v($verbose) = False ) { my Int @ints = @input>>.Int; @@ -32,7 +32,6 @@ multi MAIN (#| A list of unique integers @cur-loop.push: $cur-value; @ints[$cur-index] = Nil; - if $verbose { dd @ints; dd $start-pointer; @@ -45,7 +44,7 @@ multi MAIN (#| A list of unique integers given $next-index { when * ≥ $num-elems { - say "\e[31mFound singular loop[s]:\e[0m ", + say "\e[31mFound single-item loop[s]:\e[0m ", @cur-loop.map({"[$_]"}).join(' ') if $verbose; @all-loops.push: $_ for @cur-loop; } # end of when * ≥ $num-elems @@ -75,7 +74,7 @@ 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; + print "Number of loops: " if $verbose; say @all-loops.elems; return @all-loops.elems; @@ -103,7 +102,7 @@ multi MAIN (Bool :v(:$verbose) = False) is hidden-from-USAGE { #| 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; + note "\e[31mFound a single-item loop:\e[0m [$i]" if $verbose; say 1; } # end of multi MAIN (Int $i! @@ -126,4 +125,4 @@ multi MAIN (Bool :$test!) { for @tests { cmp-ok .<got>, .<op>, .<expected>, .<desc>; } # end of for @tests -} # end of multi MAIN (:$test!)
\ No newline at end of file +} # end of multi MAIN (:$test!) diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.sl b/challenge-236/shimon-ben-avraham/raku/ch-2.sl index 06c4b3adae..d52658d3a2 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 09:34:53 PM EDT +# Last modified: Fri 29 Sep 2023 10:26:35 PM EDT # Version 0.0.1 # begin-no-weave @@ -79,16 +79,16 @@ Loop is as below: We will create a pointer to the first index of the array and attempt to find a loop that starts with that element. -It's important to remember that each element can be a part of only I<one> loop, +It's important to remember that each element can be a part of only U<one> loop, even if it is a loop by itself. -Every time we find an element, we will push it to a loop array and set the -element to C<Nil> so that we don't use it again. +Every time we find an element, we will push it to a current-loop array and set +the element to C<Nil> so that we don't use it again. -If we find a loop, we will push it to an array of loops. If we don't find a -loop, we will move the start pointer to the next defined element and try again. - -Note that a 'loop' is defined as a list of integers, not a list of indices. +If we find a loopN<A 'loop' is defined as a list of array element values, not +a list of the array's indices.>, we will push it to an array of loops. Note +that a loop can consist of a single element. After we find a loop, we will +move the start pointer to the next defined element and try again. First we will only accept input that is a list of unique integers. @@ -116,8 +116,6 @@ multi MAIN (#| A list of unique integers The current loop we are working on is stored in C<@cur-loop>. The list of all found loops is stored in C<@all-loops>. -Note that a loop can consist of a single element. - =end pod my @cur-loop = []; @@ -152,7 +150,6 @@ set to C<Nil> so that we don't use it again. @cur-loop.push: $cur-value; @ints[$cur-index] = Nil; - #begin-no-weave if $verbose { dd @ints; @@ -183,7 +180,7 @@ all loops. when * ≥ $num-elems { #begin-no-weave - say "\e[31mFound singular loop[s]:\e[0m ", + say "\e[31mFound single-item loop[s]:\e[0m ", @cur-loop.map({"[$_]"}).join(' ') if $verbose; #end-no-weave @all-loops.push: $_ for @cur-loop; @@ -225,7 +222,7 @@ index. } # end of given $next-index =begin pod -At this point we have found a loop or singular loop[s]. We need to find the +At this point we have found a loop or single-item loop[s]. We need to find the next start pointer by looking for the next defined element in the array. =end pod @@ -259,12 +256,12 @@ next start pointer by looking for the next defined element in the array. =head2 Sample run with debug prints -(The option C<--verbose> and the debug print statemnts are not shown in the +(The option C<--verbose> and the debug print statements are not shown in the above code.) =begin code :lang<sh> -./ch-2.raku --verbose 1 0 8 5 4 3 9 +$ ./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 @@ -288,7 +285,7 @@ Int $cur-index = 2 Int $next-index = 8 Int $cur-value = 8 Array @cur-loop = [8] -Found singular loop[s]: [8] +Found single-item loop[s]: [8] Starting new loop at index 3 Array[Int] @ints = Array[Int].new(Int, Int, Int, Int, 4, 3, 9) @@ -322,7 +319,7 @@ Int $cur-index = 6 Int $next-index = 9 Int $cur-value = 9 Array @cur-loop = [9] -Found singular loop[s]: [9] +Found single-item loop[s]: [9] All loops: @@ -344,7 +341,7 @@ Number of loops: 5 Shimon Bollinger (deoac.shimon@gmail.com) -=comment Source can be located at: +=comment The complete source can be located at: Z<Challenge 236|https://github.com/deoac/perlweeklychallenge-club/tree/master/challenge-236/shimon-ben-avraham raku> Comments and Pull Requests are welcome. @@ -385,7 +382,7 @@ multi MAIN (Bool :v(:$verbose) = False) is hidden-from-USAGE { #| 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; + note "\e[31mFound a single-item loop:\e[0m [$i]" if $verbose; say 1; } # end of multi MAIN (Int $i! |
