Words and numbers

Weekly Challenge 319 Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. It's a great way for us all to practice some coding. Challenge, My solutions Task 1: Word Count Task 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. My solution So vowels in English are a, e, i, o, u and sometimes y. It seems clear from the second example that even though weekly ends with a vowel, the task assumes that it is just the five letters. This task is a one liner, and simply counts the words that starts with a vowel letter or ends with one. def word_count(words: list[str]) -> int: return sum(1 for word in words if re.search('^[aeiou]', word) or re.search('[aeiou]$', word)) Examples $ ./ch-1.py unicode xml raku perl 2 $ ./ch-1.py the weekly challenge 2 $ ./ch-1.py perl python postgres 0 Task 2: Minimum Common Task You are given two arrays of integers. Write a script to return the minimum integer common to both arrays. If none found return -1. My solution For the command line input, I take two strings and separate them on non-digit characters. My solution takes the two lists and converts them to sets and uses the intersection method & to get values that appears in both lists. If there is one or more values found it returns the minimum value. If none is found, it will return -1. def minimum_common(list1: list[int], list2: list[int]) -> int: intersection = set(list1) & set(list2) return min(intersection) if intersection else -1 Examples $ ./ch-2.py "1, 2, 3, 4" "3, 4, 5, 6" 3 $ ./ch-2.py "1, 2, 3" "2, 4" 2 $ ./ch-2.py "1, 2, 3, 4" "5, 6, 7, 8" -1

May 4, 2025 - 13:17
 0
Words and numbers

Weekly Challenge 319

Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. It's a great way for us all to practice some coding.

Challenge, My solutions

Task 1: Word Count

Task

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.

My solution

So vowels in English are a, e, i, o, u and sometimes y. It seems clear from the second example that even though weekly ends with a vowel, the task assumes that it is just the five letters.

This task is a one liner, and simply counts the words that starts with a vowel letter or ends with one.

def word_count(words: list[str]) -> int:
    return sum(1 for word in words if re.search('^[aeiou]', word) or re.search('[aeiou]$', word))

Examples

$ ./ch-1.py unicode xml raku perl
2

$ ./ch-1.py the weekly challenge
2

$ ./ch-1.py perl python postgres
0

Task 2: Minimum Common

Task

You are given two arrays of integers.

Write a script to return the minimum integer common to both arrays. If none found return -1.

My solution

For the command line input, I take two strings and separate them on non-digit characters.

My solution takes the two lists and converts them to sets and uses the intersection method & to get values that appears in both lists.

If there is one or more values found it returns the minimum value. If none is found, it will return -1.

def minimum_common(list1: list[int], list2: list[int]) -> int:
    intersection = set(list1) & set(list2)
    return min(intersection) if intersection else -1

Examples

$ ./ch-2.py "1, 2, 3, 4" "3, 4, 5, 6"
3

$ ./ch-2.py "1, 2, 3" "2, 4"
2

$ ./ch-2.py "1, 2, 3, 4" "5, 6, 7, 8"
-1