Task 1: String Alike
"Even Split: Do Both Halves Have the Same Vowel Count?"
This week's first task gives us an even-length string and asks whether it can be split into two equal halves that each contain the same non-zero number of vowels. A vowel is any of A, E, I, O, U (case-insensitive).
The Strategy: Split the string in the middle and count vowels in each half using a simple transliteration or regex. Return true only when both counts are equal AND greater than zero. If either half has no vowels, or the counts differ, return false.
Perl Implementation
sub string_alike ($text) {
($text) = $STRING_CHECK->($text);
my $length = length $text;
die 'Expected an even-length string' if $length % 2;
my $half = $length / 2;
my $first_half = substr $text, 0, $half;
my $second_half = substr $text, $half;
my $first_count = ( $first_half =~ tr/AEIOUaeiou// );
my $second_count = ( $second_half =~ tr/AEIOUaeiou// );
return $first_count > 0 && $first_count == $second_count;
}
Python Implementation
def string_alike(text: str) -> bool:
"""Return True if both halves have the same non-zero vowel count."""
if len(text) % 2:
raise ValueError("Expected an even-length string")
half = len(text) // 2
vowels = set("AEIOUaeiou")
first_count = sum(1 for ch in text[:half] if ch in vowels)
second_count = sum(1 for ch in text[half:] if ch in vowels)
return first_count > 0 and first_count == second_count