Robbie Hatley's Solutions To The Weekly Challenge #263

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

This week (2024-03-31 through 2024-04-06) is weekly challenge #263. Its tasks are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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

Function "indexes" from CPAN module "List::MoreUtils" solves this easily:

   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;
   }

Robbie Hatley's Perl Solution to The Weekly Challenge 263-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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] ]

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":

   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;
   }

Robbie Hatley's Perl Solution to The Weekly Challenge 263-2

That's it for challenge 263; see you on challenge 264!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #262

Robbie Hatley's Solutions To The Weekly Challenge #266

Robbie Hatley's Solutions To The Weekly Challenge #273