Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #319 (“Word Count” and “Minimum Common”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of taks each Monday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2025-04-28 through 2025-05-04 is #319

The tasks for challenge #319 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 319-1: Word Count
Submitted by: Mohammad Sajid Anwar
You are given a list of words containing alphabetic characters
only. Write a script to return the count of words either
starting with a vowel or ending with a vowel.

Example #1:
Input: @list = ("unicode", "xml", "raku", "perl")
Output: 2
The words are "unicode" and "raku".

Example #2:
Input: @list = ("the", "weekly", "challenge")
Output: 2

Example #3:
Input: @list = ("perl", "python", "postgres")
Output: 0

I'll interpret "alphabetic character" to mean any character matching "\pL". But that includes alphabets where the characters don't correspond to "consonants" or "vowels", but rather, to syllables or even ideas.

What's a "vowel"? In English, Spanish, French, and German, a vowel is anything which, when stripped of diacritical marks and case-folded, matches "[aeiou]". But Greek and Russian also have vowels, and so do many other languages. In fact, 65 written languages have "alphabets" consisting of "vowels" and "consonants". But most of them are either little-used languages or languages with characters I can't recognize. So for the purpose of this exercise, I'll consider only English, Greek, and Russian vowels: [aeiou] [аеёиоуыэюя] [αεηιου]. And Russian "ё" decomposes and the double-umlaut will be stripped, so my "vowel" collection is [aeiouаеиоуыэюяαεηιου].

The rest is just a matter of decomposing, stripping, case-folding, and comparing first and last characters to my vowel collection using a regular expression in a m// operator.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 319-2: Minimum Common
Submitted by: Mohammad Sajid Anwar
You are given two arrays of integers. Write a script to return
the minimum integer common to both arrays. If none found,
return -1.

Example #1:
Input: @array_1 = (1, 2, 3, 4)
       @array_2 = (3, 4, 5, 6)
Output: 3
The common integer in both arrays: 3, 4
The minimum is 3.

Example #2:
Input: @array_1 = (1, 2, 3)
       @array_2 = (2, 4)
Output: 2

Example #3:
Input: @array_1 = (1, 2, 3, 4)
       @array_2 = (5, 6, 7, 8)
Output: -1

I disagree with the part about "if none found, return -1", becuase -1 is a valid possible "minimum common integer". (Negative integers are "integers".) So I'll return undef if no "minimum common integer" is found.

The rest is just a matter of flagging the common integers (which I do by using a hash and bitwise OR), then sorting the combined integers from both arrays by ascending numerical value, then using function "first" from CPAN module "List::Util" to find the first (hence least) integer for which the hash value is 3 (indicating that the integer is in both arrays).

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

That's it for challenge 319; see you on challenge 320!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #317 (Theme: “Friendly Acronyms”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #318 (“Group Positions” and “Reverse Equals”)