aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-26 11:52:11 +0000
committerGitHub <noreply@github.com>2024-03-26 11:52:11 +0000
commit6b5e4c8ab3a769e4aac7b512e2248af0d62409c0 (patch)
tree7a05f1cd86b6d36fb3be1e369869337ec51066fd
parent881a3b3023f9fe20ad3e169ae6599f97d66fee82 (diff)
parent964cce285bb10515ca71b00cd1c39c0e756ea135 (diff)
downloadperlweeklychallenge-club-6b5e4c8ab3a769e4aac7b512e2248af0d62409c0.tar.gz
perlweeklychallenge-club-6b5e4c8ab3a769e4aac7b512e2248af0d62409c0.tar.bz2
perlweeklychallenge-club-6b5e4c8ab3a769e4aac7b512e2248af0d62409c0.zip
Merge pull request #9821 from robbie-hatley/rh262
Robbie Hatley's solutions in Perl for The Weekly Challenge #262.
-rw-r--r--challenge-262/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-262/robbie-hatley/perl/ch-1.pl88
-rwxr-xr-xchallenge-262/robbie-hatley/perl/ch-2.pl84
3 files changed, 173 insertions, 0 deletions
diff --git a/challenge-262/robbie-hatley/blog.txt b/challenge-262/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..2e1036d90a
--- /dev/null
+++ b/challenge-262/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2024/03/robbie-hatleys-solutions-to-weekly_26.html \ No newline at end of file
diff --git a/challenge-262/robbie-hatley/perl/ch-1.pl b/challenge-262/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..2ec01c7fb9
--- /dev/null
+++ b/challenge-262/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,88 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 262-1,
+written by Robbie Hatley on Mon Mar 25, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 262-1: Max Positive Negative
+Submitted by: Mohammad Sajid Anwar
+You are given an array of integers, @ints. Write a script to
+return the maximum number of either positive or negative
+integers in the given array.
+
+Example 1:
+Input: @ints = (-3, 1, 2, -1, 3, -2, 4)
+Output: 4
+Count of positive integers: 4
+Count of negative integers: 3
+Maximum of count of positive and negative integers: 4
+
+Example 2:
+Input: @ints = (-1, -2, -3, 1)
+Output: 3
+Count of positive integers: 1
+Count of negative integers: 3
+Maximum of count of positive and negative integers: 3
+
+Example 3:
+Input: @ints = (1,2)
+Output: 2
+Count of positive integers: 2
+Count of negative integers: 0
+Maximum of count of positive and negative integers: 2
+
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I'll use "grep" to get lists of all positive and negative numbers, "scalar" to get the sizes of those lists,
+then return the negative count if it's the larger, else return the positive count.
+
+--------------------------------------------------------------------------------------------------------------
+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,-7,4,-5,6],[-5,0,-4,-3,0,-2,-1,0,1,2])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+use v5.36;
+sub max_pos_neg (@a) {
+ my $pos = scalar grep {$_ > 0} @a;
+ my $neg = scalar grep {$_ < 0} @a;
+ if ($neg > $pos) {return $neg} else {return $pos}
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ [-3, 1, 2, -1, 3, -2, 4],
+ # Expected Output: 4
+
+ # Example 2 Input:
+ [-1, -2, -3, 1],
+ # Expected Output: 3
+
+ # Example 3 Input:
+ [1,2],
+ # Expected Output: 2
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+for my $aref (@arrays) {
+ say '';
+ say 'Array = (', join(', ', @$aref), ')';
+ say 'Max Pos/Neg = ', max_pos_neg(@$aref);
+}
diff --git a/challenge-262/robbie-hatley/perl/ch-2.pl b/challenge-262/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..eb14f12bbe
--- /dev/null
+++ b/challenge-262/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,84 @@
+#!/usr/bin/env -S perl -CSDA
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 262-2,
+written by Robbie Hatley on Mon Mar 25, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 262-2: Count Equal Divisible
+Submitted by: Mohammad Sajid Anwar
+You are given an array of integers, @ints, and an integer $k.
+Write a script to return the number of pairs (i, j) where
+a) 0 <= i < j < size of @ints
+b) ints[i] == ints[j]
+c) i x j is divisible by k
+
+Example 1
+Input: @ints = (3,1,2,2,2,1,3) and $k = 2
+Output: 4
+(0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2
+(2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2
+(2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2
+(3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2
+
+Example 2
+Input: @ints = (1,2,3) and $k = 1
+Output: 0
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+Requirement (a) is easily implementable with a pair of nested 3-part loops.
+Requirement (b) is just an integer equality check, implementable with "==".
+Requirement (c) can be implemented as "0==(i*j)%k".
+
+--------------------------------------------------------------------------------------------------------------
+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 (with the last integer of each inner array being the integer "k"
+described in the problem description), in proper Perl syntax, like so:
+./ch-2.pl '([6, -17, 3, -17, 6, 17, 3, 3],[47, 82, 6, -17, 82, 2])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+use v5.36;
+sub count_eq_div ($k, @a) {
+ my $c = 0;
+ for (my $i = 0 ; $i <= $#a-1 ; ++$i) {
+ for (my $j = $i + 1 ; $j <= $#a-0 ; ++$j) {
+ if ($a[$i]==$a[$j] && 0==($i*$j)%$k) {++$c}
+ }
+ }
+ return $c;
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ [3,1,2,2,2,1,3,2],
+ # Expected Output: 4
+
+ # Example 2 Input:
+ [1,2,3,1],
+ # Expected Output: 0
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+for my $aref (@arrays) {
+ say '';
+ my @array = @$aref;
+ my $k = pop @array;
+ say 'Array = (', join(', ', @array), ')';
+ say "Count of equal pairs with index products divisible by $k = ", count_eq_div($k,@array);
+}