Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #310 ("Arrays Intersection" and "Sort Odd Even")
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 for the week of 2025-02-24 through 2025-03-02 is #310.
The tasks for challenge #310 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 310-1: Arrays Intersection Submitted by: Mohammad Sajid Anwar You are given a list of arrays of integers. Write a script to return the common elements in all the arrays. Example #1: Input: $list = ( [1, 2, 3, 4], [4, 5, 6, 1], [4, 2, 1, 3] ) Output: (1, 4) Example #2: Input: $list = ( [1, 0, 2, 3], [2, 4, 5] ) Output: (2) Example #3: Input: $list = ( [1, 2, 3], [4, 5], [6] ) Output: ()
I'll start by writing a subroutine that determines whether a given integer is in a given array of integers. Then I'll write a sub that determines whether a given integer is in all of the given arrays. Finally, I'll write a sub that determines the common elements between multiple arrays.

Robbie Hatley's Perl Solution to The Weekly Challenge 310-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 310-2: Sort Odd Even Submitted by: Mohammad Sajid Anwar You are given an array of integers. Write a script to sort odd index elements in decreasing order and even index elements in increasing order in the given array. Example #1: Input: @ints = (4, 1, 2, 3) Output: (2, 3, 4, 1) Even index elements: 4, 2 => 2, 4 (increasing order) Odd index elements : 1, 3 => 3, 1 (decreasing order) Example #2: Input: @ints = (3, 1) Output: (3, 1) Example #3: Input: @ints = (5, 3, 2, 1, 4) Output: (2, 3, 4, 1, 5) Even index elements: 5, 2, 4 => 2, 4, 5 (increasing order) Odd index elements : 3, 1 => 3, 1 (decreasing order)
I'll first separate the odd and even elements into to separate arrays, sort them separately (odds decending, evens ascending), then use the "mesh" function from CPAN module "List::SomeUtils" to mesh them together.

Robbie Hatley's Perl Solution to The Weekly Challenge 310-2
That's it for challenge 310; see you on challenge 311!
Comments
Post a Comment