diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-03-18 22:04:29 -0700 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-03-18 22:04:29 -0700 |
| commit | dff3a8f8e00a2328d579b3e7f39b332fe0983624 (patch) | |
| tree | 340b4efeab6607bace1a1859d855f1883a1eefed | |
| parent | d40239917330ad0a9e6095dceb6c69ba4b7ca6c6 (diff) | |
| download | perlweeklychallenge-club-dff3a8f8e00a2328d579b3e7f39b332fe0983624.tar.gz perlweeklychallenge-club-dff3a8f8e00a2328d579b3e7f39b332fe0983624.tar.bz2 perlweeklychallenge-club-dff3a8f8e00a2328d579b3e7f39b332fe0983624.zip | |
Robbie Hatley's corrections to Perl scripts for The Weekly Challenge #261.
| -rwxr-xr-x | challenge-261/robbie-hatley/perl/ch-1.pl | 25 | ||||
| -rwxr-xr-x | challenge-261/robbie-hatley/perl/ch-2.pl | 19 |
2 files changed, 6 insertions, 38 deletions
diff --git a/challenge-261/robbie-hatley/perl/ch-1.pl b/challenge-261/robbie-hatley/perl/ch-1.pl index 7e1a1a79b2..3ea5b8c3de 100755 --- a/challenge-261/robbie-hatley/perl/ch-1.pl +++ b/challenge-261/robbie-hatley/perl/ch-1.pl @@ -44,30 +44,7 @@ Output: 1296 -------------------------------------------------------------------------------------------------------------- PROBLEM NOTES: I'll use "sum0" from "List::Util" to sum number lists, "split" and "grep" to get the digits, and "abs" to get -the absolute value of the difference between the element and digit sums: - - use v5.38; - use utf8; - use List::Util 'sum0'; - - # Return the sum of the elements of any array of - # decimal representations of real numbers: - sub el_sum (@array) { - return sum0 @array; - } - - # Return the sum of the digits of any array of - # decimal representations of real numbers: - sub di_sum (@array) { - return sum0 grep {$_ =~ /^[0-9]$/} map {split //, $_} @array; - } - - # Return the absolute value of the difference between - # the element sum and the digit sum of any array of - # decimal representations of real numbers: - sub abs_diff_el_di (@array) { - return abs(el_sum(@array)-di_sum(@array)); - } +the absolute value of the difference between the element and digit sums. -------------------------------------------------------------------------------------------------------------- IO NOTES: diff --git a/challenge-261/robbie-hatley/perl/ch-2.pl b/challenge-261/robbie-hatley/perl/ch-2.pl index 2e75fb58c6..46cd0190f6 100755 --- a/challenge-261/robbie-hatley/perl/ch-2.pl +++ b/challenge-261/robbie-hatley/perl/ch-2.pl @@ -13,7 +13,7 @@ Task 261-2: Multiply by Two Submitted by: Mohammad Sajid Anwar Reworded for clarity by Robbie Hatley You are given an array of integers, @ints, and an integer, $start. -Write a script to do the followings: +Write a script to do the following: 1. Look for $start in the array @ints; if found, multiply the value of $start by 2 in-situ. 2. If not found, stop the process; otherwise, repeat. @@ -43,20 +43,11 @@ Output: 2 -------------------------------------------------------------------------------------------------------------- PROBLEM NOTES: I'll use "any" from "List::Util" to determine whether $start is in @array, and "while" to repeatedly -double $start for so long as $start is in @array: +double $start for so long as $start is in @array. - use v5.38; - use utf8; - use List::Util 'any'; - - # Double $start while $start is in @array: - sub mult_by_two ($start, @array) { - return 0 if 0 == $start; - return ($start *= 2 while any {$_ == $start} @array); - } - -Warning: If $start is 0, and 0 is also in @array, sub "mult_by_two" will loop indefinitely, so precaution -must be taken to ensure that $start is never 0. +Warning: If $start is 0, and 0 is also in @array, any loop-based procedure for determining the final value of +$start will loop indefinitely, because "$start *= 2" will always produce "0", which will always still be in +the array. So precaution should be taken to ensure that $start is never 0. -------------------------------------------------------------------------------------------------------------- IO NOTES: |
