Advent Calendar - December 9, 2024

Monday, Dec 9, 2024| Tags: Perl

Advent Calendar 2024

|   Day 8   |   Day 9   |   Day 10   |


The gift is presented by James Smith. Today he is talking about his solution to The Weekly Challenge - 263. This is re-produced for Advent Calendar 2024 from the original post.



The Weekly Challenge - 263



Task #1: Target Index



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.

Solution



It looks like we need to sort - BUT WE DON'T - we know that the first index will be the number of numbers less than $k, and the number of matching numbers is the number of numbers equal to $k.

Like last week, we use the spaceship operator to compute the values larger/smaller than $k.

If we create the counts array (0) x 3, then the number smaller are in $c[-1] OR $c[2], the number the same are in $c[0].

So we just need to output the list of numbers starting at $c[2] of length $c[0]


sub target_index {
  my( $k, @c ) = ( pop, 0, 0, 0 );
  $c[ $_ <=> $k ]++ for @_;
  $c[2] .. $c[2] + $c[0] - 1
}

Spaceship operator <=> returns values -1,0,1 depending on the sign of the difference.


Task #2: Merge Items



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.

Solution



We don’t need to know which group the count is from - in fact the code below works if you have 1, 2, 3 or even 100 lists.

We loop through every pair in every list updating the count for the key…

We code simplify the code to use:


$code{ $_->[0] } += $_->[1] for map { @{$_} } @_;

but this could have memory issues as you have to create a new array and loop over it. So we use two loops instead. One example of where code golf can cause a major inefficiency.

We store the values of the hash and return the keys and values as a list of pairs rather than the normal hash structure.


sub merge_items {
  my %c;
  for( @_ ) {
    $c{ $_->[0] } += $_->[1] for @{$_}
  }
  map { [ 0 + $_ => $c{$_} ] } keys %c
}


If you have any suggestion then please do share with us perlweeklychallenge@yahoo.com.

|   Advent Calendar 2024   |

SO WHAT DO YOU THINK ?

If you have any suggestions or ideas then please do share with us.

Contact with me