aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-19 15:09:03 +0000
committerGitHub <noreply@github.com>2024-03-19 15:09:03 +0000
commit08ac257a575433f6a0478525a114b440efdc8ff5 (patch)
treed01003b6c99b2857430ee0908d5b0958341ea4e0
parent39ce01714ffd9bc7c3366eeff215da4e1a902b3e (diff)
parentdff3a8f8e00a2328d579b3e7f39b332fe0983624 (diff)
downloadperlweeklychallenge-club-08ac257a575433f6a0478525a114b440efdc8ff5.tar.gz
perlweeklychallenge-club-08ac257a575433f6a0478525a114b440efdc8ff5.tar.bz2
perlweeklychallenge-club-08ac257a575433f6a0478525a114b440efdc8ff5.zip
Merge pull request #9776 from robbie-hatley/rh261
Robbie Hatley's solutions in Perl for The Weekly Challenge #261
-rw-r--r--challenge-261/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-261/robbie-hatley/perl/ch-1.pl115
-rwxr-xr-xchallenge-261/robbie-hatley/perl/ch-2.pl108
3 files changed, 224 insertions, 0 deletions
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..3ea5b8c3de
--- /dev/null
+++ b/challenge-261/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,115 @@
+#!/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.
+
+--------------------------------------------------------------------------------------------------------------
+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..46cd0190f6
--- /dev/null
+++ b/challenge-261/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,108 @@
+#!/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 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.
+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.
+
+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:
+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);
+}