The Weekly Challenge 319

Vowel Boundaries & Smallest Common Elements!

Original Challenge Link

Task 1: Word Count

"Vowel Landmarks: Counting Words with Vocalic Boundaries!"

Given a list of words, count how many of them either start or end with a vowel (a, e, i, o, u).

The Strategy: Iterate through each word in the list. For each word, check if the first character or the last character matches a predefined set of vowels (case-insensitive). Increment a counter whenever a match is found and return the total count.
Perl Implementation
sub word_count ($words) {
    my $count = 0;
    for my $word (@$words) {
        ++$count if $word =~ /\A[aeiou]/i || $word =~ /[aeiou]\z/i;
    }
    return $count;
}
Python Implementation
def word_count(words: Sequence[str]) -> int:
    VOWELS = set("aeiou")
    count = 0
    for word in words:
        if not word: continue
        lower = word.lower()
        if lower[0] in VOWELS or lower[-1] in VOWELS:
            count += 1
    return count

Task 2: Minimum Common

"Smallest Intersection: Finding the Least Shared Integer!"

Identify the smallest integer that exists in two provided arrays. If no common integer is found, return -1.

The Strategy: Use a set-based intersection approach. In Python, the set.intersection() method makes this trivial. In Perl, we can use a hash to represent one set and then grep through the other array to find shared elements. Finally, return the minimum of the common elements, or -1 if the intersection is empty.
Perl Implementation
sub minimum_common ($a, $b) {
    my %in_b = map { $_ => 1 } @$b;
    my @common = grep { $in_b{$_} } @$a;
    return -1 if !@common;
    return min(@common);
}
Python Implementation
def minimum_common(array_1: Sequence[int], array_2: Sequence[int]) -> int:
    common = set(array_1).intersection(array_2)
    return min(common) if common else -1