From d40239917330ad0a9e6095dceb6c69ba4b7ca6c6 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Mon, 18 Mar 2024 21:54:29 -0700 Subject: Robbie Hatley's solutions in Perl for The Weekly Challenge #261 --- challenge-261/robbie-hatley/blog.txt | 1 + challenge-261/robbie-hatley/perl/ch-1.pl | 138 +++++++++++++++++++++++++++++++ challenge-261/robbie-hatley/perl/ch-2.pl | 117 ++++++++++++++++++++++++++ 3 files changed, 256 insertions(+) create mode 100644 challenge-261/robbie-hatley/blog.txt create mode 100755 challenge-261/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-261/robbie-hatley/perl/ch-2.pl diff --git a/challenge-261/robbie-hatley/blog.txt b/challenge-261/robbie-hatley/blog.txt new file mode 100644 index 0000000000..1f043695c5 --- /dev/null +++ b/challenge-261/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2024/03/robbie-hatleys-solutions-to-weekly_18.html \ No newline at end of file diff --git a/challenge-261/robbie-hatley/perl/ch-1.pl b/challenge-261/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..7e1a1a79b2 --- /dev/null +++ b/challenge-261/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,138 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 261-1, +written by Robbie Hatley on Mon Mar 18, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 261-1: Element Digit Sum +Submitted by Mohammad Sajid Anwar +Reworded for clarity by Robbie Hatley. +You are given an array of integers, @ints. Write a script to +evaluate the absolute value of the difference between the +element and digit sums of @ints. + +Example 1: +Input: @ints = (1,2,3,45) +Output: 36 +Element Sum: 1 + 2 + 3 + 45 = 51 +Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +Absolute Difference: | 51 - 15 | = 36 + +Example 2: +Input: @ints = (1,12,3) +Output: 9 +Element Sum: 1 + 12 + 3 = 16 +Digit Sum: 1 + 1 + 2 + 3 = 7 +Absolute Difference: | 16 - 7 | = 9 + +Example 3: +Input: @ints = (1,2,3,4) +Output: 0 +Element Sum: 1 + 2 + 3 + 4 = 10 +Digit Sum: 1 + 2 + 3 + 4 = 10 +Absolute Difference: | 10 - 10 | = 0 + +Example 4: +Input: @ints = (236, 416, 336, 350) +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)); + } + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays of decimal representations of real numbers, in proper Perl syntax, like so: +./ch-1.pl '([2, 3.8, -1232.01, 10],[9, 8, 7, 6])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + +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)); +} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 Input: + [1,2,3,45], + # Expected Output: 36 + + # Example 2 Input: + [1,12,3], + # Expected Output: 9 + + # Example 3 Input: + [1,2,3,4], + # Expected Output: 0 + + # Example 4 Input: + [236, 416, 336, 350], + # Expected Output: 1296 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +for my $aref (@arrays) { + say ''; + my @array = @$aref; + say 'Array = (', join(', ', @array), ')'; + say 'Sum of elements = ', el_sum(@array); + say 'Sum of digits = ', di_sum(@array); + say 'Absolute value of difference between element and digit sums = ', abs_diff_el_di (@array); +} diff --git a/challenge-261/robbie-hatley/perl/ch-2.pl b/challenge-261/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..2e75fb58c6 --- /dev/null +++ b/challenge-261/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,117 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 261-2, +written by Robbie Hatley on Mon Mar 18, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +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: +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. +3. Return the final value of $start. + +Example 1: +Input: @ints = (5,3,6,1,12) and $start = 3 +Output: 24 +Step 1: 3 is in the array so 3 x 2 = 6 +Step 2: 6 is in the array so 6 x 2 = 12 +Step 3: 12 is in the array so 12 x 2 = 24 +24 is not found in the array so return 24. + +Example 2: +Input: @ints = (1,2,4,3) and $start = 1 +Output: 8 +Step 1: 1 is in the array so 1 x 2 = 2 +Step 2: 2 is in the array so 2 x 2 = 4 +Step 3: 4 is in the array so 4 x 2 = 8 +8 is not found in the array so return 8. + +Example 3: +Input: @ints = (5,6,7) and $start = 2 +Output: 2 +2 is not found in the array so return 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: + + 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. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays of decimal representations of integers, in proper Perl syntax, like so: +./ch-2.pl '([-1600, 2, -400, 7, -800, 4, -400], [9, 8, 7, 6, 13])' +The last member of each inner array will be construed as "$start", the remainder as "@ints", as per the +problem description. + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + +use v5.38; +use utf8; +use List::Util 'any'; + +# Double $start while $start is in @array: +sub mult_by_two ($start, @array) { + $start *= 2 while any {$_ == $start} @array; + return $start; +} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1: + [5,3,6,1,12,3], + # Expected Output: 24 + + # Example 2: + [1,2,4,3,1], + # Expected Output: 8 + + # Example 3: + [5,6,7,2], + # Expected Output: 2 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +for my $aref (@arrays) { + say ''; + my @array = @$aref; + my $start = pop @array; + say 'Array = (', join(', ', @$aref), ')'; + say 'Start = ', $start; + if ( 0 == $start ) { + say 'Error: $start may not be 0.'; + say 'Moving on to next array.'; + next; + } + say 'Finish = ', mult_by_two($start, @array); +} -- cgit From dff3a8f8e00a2328d579b3e7f39b332fe0983624 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Mon, 18 Mar 2024 22:04:29 -0700 Subject: Robbie Hatley's corrections to Perl scripts for The Weekly Challenge #261. --- challenge-261/robbie-hatley/perl/ch-1.pl | 25 +------------------------ 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: -- cgit