aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-05-09 21:24:50 +0100
committerGitHub <noreply@github.com>2025-05-09 21:24:50 +0100
commit2f536dc9a3710547c7e75c1d5636152828720646 (patch)
treeceedf03a597112afe40ade72c151f5332865d23f
parent4260922ac702246fa5e6743be89f77846ce8d96f (diff)
parent860bb0241d1dfe6912a015d534d7ab7f33c72102 (diff)
downloadperlweeklychallenge-club-2f536dc9a3710547c7e75c1d5636152828720646.tar.gz
perlweeklychallenge-club-2f536dc9a3710547c7e75c1d5636152828720646.tar.bz2
perlweeklychallenge-club-2f536dc9a3710547c7e75c1d5636152828720646.zip
Merge pull request #11993 from robbie-hatley/rh320
Robbie Hatley's solutions, in Perl, for The Weekly Challenge #320.
-rw-r--r--challenge-320/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-320/robbie-hatley/perl/ch-1.pl80
-rwxr-xr-xchallenge-320/robbie-hatley/perl/ch-2.pl80
3 files changed, 161 insertions, 0 deletions
diff --git a/challenge-320/robbie-hatley/blog.txt b/challenge-320/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..4c1ffa7cb5
--- /dev/null
+++ b/challenge-320/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2025/05/robbie-hatleys-solutions-in-perl-for.html
diff --git a/challenge-320/robbie-hatley/perl/ch-1.pl b/challenge-320/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..ea0b53b2ce
--- /dev/null
+++ b/challenge-320/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,80 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 320-1,
+written by Robbie Hatley on Mon May 05, 2025.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 320-1: Maximum Count
+Submitted by: Mohammad Sajid Anwar
+You are given an array of integers. Write a script to return the
+maximum between the number of positive and negative integers.
+Zero is neither positive nor negative.
+
+Example #1:
+Input: @ints = (-3, -2, -1, 1, 2, 3)
+Output: 3
+There are 3 positive integers.
+There are 3 negative integers.
+The maximum between 3 and 3 is 3.
+
+Example #2:
+Input: @ints = (-2, -1, 0, 0, 1)
+Output: 2
+There are 1 positive integers.
+There are 2 negative integers.
+The maximum between 2 and 1 is 2.
+
+Example #3:
+Input: @ints = (1, 2, 3, 4)
+Output: 4
+There are 4 positive integers.
+There are 0 negative integers.
+The maximum between 4 and 0 is 4.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+To solve this problem, I'll grep for negatives, and grep for positives, and return the max between the scalars
+of those two lists.
+
+--------------------------------------------------------------------------------------------------------------
+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 integers, in proper Perl syntax, like so:
+
+./ch-1.pl '([-2,-1,0,1,2,3],[-1,5,-37,-42,3,-89,-13,54,-8,-17])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+ use v5.36;
+ use List::Util 'max';
+ # What is max(count of neg. ints, count of pos. ints) in an array?
+ sub mc($aref){max(scalar(grep{$_<0}@$aref),scalar(grep{$_>0}@$aref))}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ [-3, -2, -1, 1, 2, 3], # 3
+ [-2, -1, 0, 0, 1], # 2
+ [1, 2, 3, 4], # 4
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+$"=', ';
+for my $aref (@arrays) {
+ say '';
+ say "Array = (@$aref)";
+ my $mc = mc($aref);
+ say "Max Count = $mc";
+}
diff --git a/challenge-320/robbie-hatley/perl/ch-2.pl b/challenge-320/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..afc37db4c5
--- /dev/null
+++ b/challenge-320/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,80 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge ###-2,
+written by Robbie Hatley on Mon May 05, 2025.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 320-2: Sum Difference
+Submitted by: Mohammad Sajid Anwar
+You are given an array of positive integers. Write a script to
+return the absolute difference between digit sum and element sum
+of the given array.
+
+Example #1:
+Input: @ints = (1, 23, 4, 5)
+Output: 18
+Element sum: 1 + 23 + 4 + 5 => 33
+Digit sum: 1 + 2 + 3 + 4 + 5 => 15
+Absolute difference: | 33 - 15 | => 18
+
+Example #2:
+Input: @ints = (1, 2, 3, 4, 5)
+Output: 0
+Element sum: 1 + 2 + 3 + 4 + 5 => 15
+Digit sum: 1 + 2 + 3 + 4 + 5 => 15
+Absolute difference: | 15 - 15 | => 0
+
+Example #3:
+Input: @ints = (1, 2, 34)
+Output: 27
+Element sum: 1 + 2 + 34 => 37
+Digit sum: 1 + 2 + 3 + 4 => 10
+Absolute difference: | 37 - 10 | => 27
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+To solve this problem, I'll split-and-sum the digits to get the digit sum, then sum the ints, then return the
+absolute value of the difference between those two 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 positive integers, in proper Perl syntax, like so:
+
+./ch-2.pl '([1,2,3,4,5],[1,2,3,44,55])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+ use v5.36;
+ use List::Util 'sum0';
+ # Abs(sum(elements)-sum(digits)):
+ sub sd($aref){abs(sum0(@$aref)-sum0(map{split//,$_}@$aref))}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ [1, 23, 4, 5], # 18
+ [1, 2, 3, 4, 5], # 0
+ [1, 2, 34], # 27
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+$"=', ';
+for my $aref (@arrays) {
+ say '';
+ say "Array = (@$aref)";
+ my $sd = sd($aref);
+ say "Absolute value of (element sum - digit sum) = $sd";
+}