Robbie Hatley's Solutions To The Weekly Challenge #284

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

The Weekly Challenge for the week of 2024-08-25 through 2024-08-30 is #284. Its tasks are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 284-1: Lucky Integer
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints. Write a script to
find the lucky integer if found otherwise return -1. If there
are more than one then return the largest. A lucky integer is
an integer that has a frequency in the array equal to its
value.

Abundance, pushing, and popping are involved:

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 284-2: Relative Sort
Submitted by: Mohammad Sajid Anwar
You are given two list of integers, @list1 and @list2.
The elements in the @list2 are distinct and also in @list1.
Write a script to sort the elements in @list1 such that the
relative order of items in @list1 is same as in the @list2.
Elements of @list1 which are not in @list2 should be placed
at the end of @list1 in ascending order.

Now this task is complicated! But I'll break-up the complications by dividing them into these 4 subroutines:

  1. Does an array consist purely of integers?
  2. Does an array consist purely of unique elements?
  3. Is one array a subset of another?
  4. Sort one array relative to another

My sorting sub works as follows: First create an array @list3. Then for each element $e2 of @list2, splice each element $e1 of @list1 which is equal to $e2 from @list1 and push it to @list3. Then sort the remaining elements of @list1 by acending numeric order and push them to @list3.

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

That's it for challenge 284; see you on challenge 285!

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