Robbie Hatley's Solutions To The Weekly Challenge #229
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle, usually with two parts, cycling every Sunday. You can find it here:
This week (2023-08-06 through 2023-08-12) is weekly challenge #229.
Task 1 is as follows:
Task 1: Lexicographic Order Submitted by: Mohammad S Anwar You are given an array of strings. Write a script to delete each string in which the letters do not occur in a lexicographically-sorted order, either forwards or backwards, and return a count of deletions. Example 1: Input: @str = ("abc", "bce", "cae") Output: 1 ("cae" is the only element which is not lexicographically sorted.) Example 2: Input: @str = ("yxz", "cba", "mon") Output: 2 ("yxz" and "mon" are not lexicographically sorted.)
I'll take the approach of writing a sub to sort strings forward and backward, then comparing each string to its forward and backward version, erasing each string which does not match at least one, and counting those deletions:
Robbie Hatley's Solution to The Weekly Challenge 229-1
Task 2 is as follows:
Task 2: Two out of Three Submitted by: Mohammad S Anwar You are given three arrays of integers. Write a script to return those elements which are present in at least 2 of the 3 arrays. Example 1: Input: @array1 = (1, 1, 2, 4) @array2 = (2, 4) @array3 = (4) Ouput: (2, 4) Example 2 Input: @array1 = (4, 1) @array2 = (2, 4) @array3 = (1, 2) Ouput: (1, 2, 4)
This is one of those rare Weekly Challenge problems for which I'm going to write a whole mess of subroutines. At the very least, I'll need subs to answer the following 6 questions:
1. Does a given scalar represent an integer? 2. Does a given reference refer to an array of integers? 3. Does a given reference refer to an array of 3 arrays of integers? 4. Does a given array of integers contain a given integer? 5. Given an array of 3 arrays of integers and an integer, how many of those arrays is the integer in? 6. Given an array of 3 arrays of integers, which elements appear in at-least-two of those arrays?
The later subs all depend heavily on earlier subs, so this will be a "tree-structured" design (as opposed to the "linear-procedural" paradigm of most of my Perl scripts):
Robbie Hatley's Solution to The Weekly Challenge 229-2
That's it for 229; see you on 230!
Comments
Post a Comment