aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-03 15:10:22 +0100
committerGitHub <noreply@github.com>2024-04-03 15:10:22 +0100
commit2385f97ce0a42419e213c3fb08c6f4c4cc89b989 (patch)
treec274e1335df624c5080829f5dd6cd2a9610c29bd
parente00d9e334fea3ef28b04c6e640986a3f4769583f (diff)
parent2d9d655e61a99c2a51a76f5a96a156404452c746 (diff)
downloadperlweeklychallenge-club-2385f97ce0a42419e213c3fb08c6f4c4cc89b989.tar.gz
perlweeklychallenge-club-2385f97ce0a42419e213c3fb08c6f4c4cc89b989.tar.bz2
perlweeklychallenge-club-2385f97ce0a42419e213c3fb08c6f4c4cc89b989.zip
Merge pull request #9867 from robbie-hatley/rh263
Robbie Hatley's solutions in Perl for The Weekly Challenge #263.
-rw-r--r--challenge-263/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-263/robbie-hatley/perl/ch-1.pl87
-rwxr-xr-xchallenge-263/robbie-hatley/perl/ch-2.pl109
3 files changed, 197 insertions, 0 deletions
diff --git a/challenge-263/robbie-hatley/blog.txt b/challenge-263/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..1b336aec00
--- /dev/null
+++ b/challenge-263/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2024/04/robbie-hatleys-solutions-to-weekly.html \ No newline at end of file
diff --git a/challenge-263/robbie-hatley/perl/ch-1.pl b/challenge-263/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..1e3acb5c43
--- /dev/null
+++ b/challenge-263/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,87 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 263-1,
+written by Robbie Hatley on Tue Apr 02, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 263-1: Target Index
+Submitted by: Mohammad Sajid Anwar
+You are given an array of integers @ints and a target element $k.
+Write a script to return the list of indices in the sorted array
+where the element is same as the given target element.
+
+Example 1:
+Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2
+Output: (1, 2)
+Sorted array: (1, 2, 2, 3, 4, 5)
+Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2
+
+Example 2:
+Input: @ints = (1, 2, 4, 3, 5), $k = 6
+Output: ()
+No element in the given array matching the given target.
+
+Example 3:
+Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4
+Output: (4)
+Sorted array: (1, 2, 2, 3, 4, 5)
+Target index: (4) as $ints[4] = 4
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+Function "indexes" from CPAN module "List::MoreUtils" should solve this.
+
+--------------------------------------------------------------------------------------------------------------
+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 '([1,5,8,5,7,5,3,5],[1,2,3,4,5,42])'
+The last element of each inner array will be construed as a "target" to search-for within the remaining array.
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+use v5.38;
+use List::MoreUtils 'indexes';
+sub indices ($target, @array) {
+ # Return those indices of the sorted array
+ # for which the value equals the target:
+ return indexes {$_ == $target} sort {$a <=> $b} @array;
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ [1, 5, 3, 2, 4, 2, 2],
+ # Expected Output: (1, 2)
+
+ # Example 2 Input:
+ [1, 2, 4, 3, 5, 6],
+ # Expected Output: ()
+
+ # Example 3 Input:
+ [5, 3, 2, 4, 2, 1, 4],
+ # Expected Output: (4)
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+for my $aref (@arrays) {
+ say '';
+ my @ints = @$aref;
+ my $target = pop @ints;
+ say 'Array = (', join(', ', @ints), ')';
+ say 'Target = ', $target;
+ say 'Indexes = (', join(', ', indices($target,@ints)), ')';
+}
diff --git a/challenge-263/robbie-hatley/perl/ch-2.pl b/challenge-263/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..9dc676baec
--- /dev/null
+++ b/challenge-263/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,109 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 263-2,
+written by Robbie Hatley on Tue Apr 02, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 263-2: Merge Items
+Submitted by: Mohammad Sajid Anwar
+You are given two 2-D array of positive integers, $items1 and
+$items2 where element is pair of (item_id, item_quantity).
+Write a script to return the merged items.
+
+Example 1:
+Input: $items1 = [ [1,1], [2,1], [3,2] ]
+ $items2 = [ [2,2], [1,3] ]
+Output: [ [1,4], [2,3], [3,2] ]
+Item id (1) appears 2 times: [1,1] and [1,3]. Merged item now (1,4)
+Item id (2) appears 2 times: [2,1] and [2,2]. Merged item now (2,3)
+Item id (3) appears 1 time: [3,2]
+
+Example 2:
+Input: $items1 = [ [1,2], [2,3], [1,3], [3,2] ]
+ $items2 = [ [3,1], [1,3] ]
+Output: [ [1,8], [2,3], [3,3] ]
+
+Example 3:
+Input: $items1 = [ [1,1], [2,2], [3,3] ]
+ $items2 = [ [2,3], [2,4] ]
+Output: [ [1,1], [2,9], [3,3] ]
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I'll make a hash with keys being "item_id" and values being "item_quanity". For each pair, I'll increment the
+value of key "item_id" by amount "item_quantity".
+
+--------------------------------------------------------------------------------------------------------------
+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 arrays of 2-element arrays of positive integers, in proper Perl syntax:
+./ch-2.pl '( [ [[1,8],[2,6]] , [[3,7],[2,4]] ] , [ [[1,1],[2,1]] , [[3,1],[4,1]] ] )'
+The inner-most 2-element arrays will be construed as ordered [key,value] pairs.
+The second-inner-most arrays are lists of key,value pairs.
+The third-inner-most arrays are collections of such lists which are to be merged.
+The outer-most array is the set of all such collections to be processed.
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+use v5.38;
+sub merge_items (@lists) {
+ # Make a hash of quantities keyed by id:
+ my %items;
+ # For each item (that is, for each (id,quantity) pair) in each list,
+ # add its quantity to the quantity for the corresponding id in the hash:
+ foreach my $list (@lists) {
+ foreach my $item (@$list) {
+ $items{$$item[0]} += $$item[1];
+ }
+ }
+ # Finally, return a list of all (id,quantity) pairs from the hash,
+ # sorted by the numeric values of the keys:
+ return map {[$_,$items{$_}]} sort {$a <=> $b} keys %items;
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ [
+ [ [1,1], [2,1], [3,2] ],
+ [ [2,2], [1,3] ],
+ ],
+ # Expected Output: [ [1,4], [2,3], [3,2] ]
+
+ # Example 2 Input:
+ [
+ [ [1,2], [2,3], [1,3], [3,2] ],
+ [ [3,1], [1,3] ],
+ ],
+ # Expected Output: [ [1,8], [2,3], [3,3] ]
+
+ # Example 3 Input:
+ [
+ [ [1,1], [2,2], [3,3] ],
+ [ [2,3], [2,4] ],
+ ],
+ # Expected Output: [ [1,1], [2,9], [3,3] ]
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+for my $aref (@arrays) {
+ say '';
+ my @lists = @$aref;
+ say 'Original lists of items:';
+ say '[ ' , join(', ', map {'[' . join(',', @$_) . ']'} @$_ ) , ' ]' for @lists ;
+ say 'Merged list:';
+ say '[ ' , join(', ', map {'[' . join(',', @$_) . ']'} merge_items(@lists)) , ' ]' ;
+}