From c47374cb5db0a55869272e3488d68dd6849930e6 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Tue, 26 Mar 2024 01:37:51 -0700 Subject: Robbie Hatley's solutions in Perl for The Weekly Challenge #262. --- challenge-262/robbie-hatley/perl/ch-1.pl | 88 ++++++++++++++++++++++++++++++++ challenge-262/robbie-hatley/perl/ch-2.pl | 84 ++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100755 challenge-262/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-262/robbie-hatley/perl/ch-2.pl 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); +} -- cgit From 964cce285bb10515ca71b00cd1c39c0e756ea135 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Tue, 26 Mar 2024 01:50:44 -0700 Subject: Added blog. --- challenge-262/robbie-hatley/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-262/robbie-hatley/blog.txt 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 -- cgit