aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoririfkin <ianrifkin@ianrifkin.com>2023-11-02 15:19:59 -0400
committeririfkin <ianrifkin@ianrifkin.com>2023-11-02 15:19:59 -0400
commit701bbe6c70ba04a11b669d0c0ff9f47c648abb1a (patch)
tree0f0b881b5de304aabdd0fd2e1d666b492df06fd5
parent2299a0cae293bf36b96d6add038d03b9109c3a91 (diff)
downloadperlweeklychallenge-club-701bbe6c70ba04a11b669d0c0ff9f47c648abb1a.tar.gz
perlweeklychallenge-club-701bbe6c70ba04a11b669d0c0ff9f47c648abb1a.tar.bz2
perlweeklychallenge-club-701bbe6c70ba04a11b669d0c0ff9f47c648abb1a.zip
documentation formatting edits and fixing a typo in task 1 code
-rw-r--r--challenge-241/ianrifkin/README.md28
-rw-r--r--challenge-241/ianrifkin/perl/ch-1.pl2
2 files changed, 16 insertions, 14 deletions
diff --git a/challenge-241/ianrifkin/README.md b/challenge-241/ianrifkin/README.md
index 79bab2131b..fcbd11b19e 100644
--- a/challenge-241/ianrifkin/README.md
+++ b/challenge-241/ianrifkin/README.md
@@ -34,18 +34,20 @@ A quick read of this task was overwhelming to me because it sounded like it was
I kept it simple and just explicitely created the three loops with the three specified iterators. It's very helpful to match the incrementor names in my code with the task's description.
-I started by looping through the input array with an incrementer `$i`. We know that $i is needed for the first value so the max $i is going to be 2 less than the total array length: `for (my $i = 0; $i < $nums_length - 2; $i++)`
+I started by looping through the input array with an incrementer `$i`. We know that `$i` is needed for the first value so the max `$i` is going to be 2 less than the total array length: `for (my $i = 0; $i < $nums_length - 2; $i++)`
Within this loop I just make the next loop with `$j` which is 1 more than `$i` to 1 less than the array length: `for (my $j = $i + 1; $j < $nums_length - 1; $j++)`
Finally within that loop I create the loop with `$k` which is going to be 1 more than `$j` to the end of the array: `for (my $k = $j + 1; $k < $nums_length; $k++)`
Now that I have my loops I go back to the instructions which stated:
+```
nums[j] - nums[i] == diff
and
nums[k] - nums[j] == diff
+```
-which in my Perl code is simple: `(@{$nums}[$j] - @{$nums}[$i] eq $diff && @{$nums}[$k] - @{$nums}[$j] eq $diff)`
+which in my Perl code is literally that same thing: `$nums[$j] - $nums[$i] == $diff && $nums[$k] - $nums[$j] == $diff`
Anytime the above is true it increments a counter `$total_finds`. After the loops are complete it prints the `$total_finds` counter.
@@ -88,17 +90,17 @@ Prime factors of 8 => 2, 2, 2
Prime factors of 27 => 3, 3, 3
```
-This one sounded simple enough but I needed to write WAY more code to accomplish. Though to be fair, part of the length is a lot more code commends and documentation.
+This one sounded simple enough but I needed to write WAY more code to accomplish. Though to be fair, part of the length is a lot more code comments and documentation -- critial because this quickly got confusing for me!
-I split up the work into 2 subroutines. The second one is simpler so let's talk about that first. I created a `prime_finder` to accept a number and output its prime.
+I split up the work into 2 subroutines. The second one is simpler so let's talk about that first. I created a `prime_finder()` to accept a number and output a prime factor.
-In the sub I have a for loop where the iterator goes from 2 to the square root of the number, which I calculated with the sqrt() function. I have it find the prime by taking the number and dividing by the iterator and seeing if it equals 0.
+In the sub I have a `for` loop where the iterator goes from 2 to the square root of the number, which I calculated with the `sqrt()` function. I have it find the prime by taking the number and dividing by the iterator and seeing if it equals 0.
-When it finds the prime instead of returning that value I'm returning the number / that prime iterator value so that I can use it later to calcuate the remaining values.
+When it finds the prime instead of returning that value I'm returning the number divided by that prime iterator value so that I can use it later to calcuate the remaining values.
-For example, given the input 8 it will find the value 2 then return 4 (because 8/2=4). This will allow the other subroutine to calculate the complete list: 2,2,2 for input 8.
+For example, given the input 8 it will find the value 2 then return 4 (because 8/2=4). This will allow the other subroutine to calculate the complete list of prime factors: 2,2,2 for input 8.
-The only other things that `prime_finder` does is if $num % $i does not equal to 0 in any of the loops then that means that the input number was actually prime so it will return the input number.
+The only other things that `prime_finder` does is if `$num % $i` does not equal to 0 in any of the loops then that means that the input number was actually prime so it will return the input number.
```
sub prime_finder {
@@ -123,18 +125,18 @@ The subroutine that calls the above and does the sorting and output printing too
The sub accepts the input array of numbers and loops through each one. It creates a hash `%results` to store the count of prime factors for each number.
-The loop starts by setting `$results{$num} = 1;` because every number will have at least the prime factor of itself.
+The loop starts by setting `$results{$num} = 1;` because every number will have at least have the prime factor of itself.
-Next I use the `prime_finder()` sub on the input number to get the value.
+Next I use the `prime_finder()` sub on the input number to get the value I decribed.
If the `prime_finder()` value is undefined or equal to the input number, we're done and can proceed to the next number in the loop.
-If `prime_finder()` returned a new number I increment `$results{$num}` to account for the smallest factor than loop to find the remaining factors, if applicable.
+If `prime_finder()` returned a new number, then I increment `$results{$num}` to account for the smallest factor than loop to find the remaining factors, if applicable.
The loop goes from the initial value returned by `prime_finder()` to the value of the input number devided by that `prime_finder()` value, which should be the smallest factor (hence why I already incremented for the smallest factor above).
-In the loop I calculate the next prime with `prime_finder()` then either increment the counter or exit the loop if no new prime is found. If a prime was found, in addition to incrementing the counter I then loop back through using the new `prime_finder()` value.
+In the loop, I calculate the next prime with `prime_finder()` then either increment the counter or exit the loop if no new prime is found. If a prime was found, in addition to incrementing the counter I then loop back through using the new `prime_finder()` value.
-With all the looping complete I sort the `%results` hash first by its values (the counter) then do a secondary sort on the key (the input number), which I save in an array. I am assuming that `tie-breaking by ascending value` mean the value of the input number. At the end I just print the `@sorted_output` array.
+With all the (potentially mind-boggling) looping complete I sort the `%results` hash first by its values (the counter) then do a secondary sort on the key (the input number), which I save in an array. I am assuming that `tie-breaking by ascending value` mean the value of the input number. At the end I just print the `@sorted_output` array.
The full code with comments is available at https://github.com/ianrifkin/perlweeklychallenge-club/blob/ianrifkin-challenge-241/challenge-241/ianrifkin/perl/ch-2.pl \ No newline at end of file
diff --git a/challenge-241/ianrifkin/perl/ch-1.pl b/challenge-241/ianrifkin/perl/ch-1.pl
index 2fdc858d18..2b9d7a6d8e 100644
--- a/challenge-241/ianrifkin/perl/ch-1.pl
+++ b/challenge-241/ianrifkin/perl/ch-1.pl
@@ -26,7 +26,7 @@ sub find_triplets {
for (my $i = 0; $i < $nums_length - 2; $i++) {
for (my $j = $i + 1; $j < $nums_length - 1; $j++) {
for (my $k = $j + 1; $k < $nums_length; $k++) {
- if (@{$nums}[$j] - @{$nums}[$i] eq $diff && @{$nums}[$k] - @{$nums}[$j] eq $diff) {
+ if (@{$nums}[$j] - @{$nums}[$i] == $diff && @{$nums}[$k] - @{$nums}[$j] == $diff) {
$total_finds++;
}
}