Task 1: Find Words
"Index Hunt: Where's the Character?"
Given a list of words and a character, return the indices (0-based) of all words that contain the given character.
The Strategy: Iterate over the list of words with their indices. For each word, check if the given character appears in it (using index or contains). Collect all matching indices and return them as an array.
Perl Implementation
sub find_words ($char, $words) {
( $char, $words ) = $ARGS_CHECK->( $char, $words );
die 'Expected a single character' if length($char) != 1;
my @idx;
for my $i ( 0 .. $#$words ) {
push @idx, $i if index( $words->[$i], $char ) >= 0;
}
return \\@idx;
}
Python Implementation
def find_words(char: str, words: list[str]) -> list[int]:
"""Return indices of words containing the given character."""
if len(char) != 1:
raise ValueError("Expected a single character")
return [i for i, w in enumerate(words) if char in w]