Weekly Challenge 319
Task 1: Word Count
This is the complete function with doctests:
#!/usr/bin/env python3
def word_count(words):
""" Given a list of words containing alphabetic characters only, return the
count of words either starting with a vowel or ending with a vowel.
>>> word_count(["unicode", "xml", "raku", "perl"])
2
>>> word_count(["the", "weekly", "challenge"])
2
>>> word_count(["perl", "python", "postgres"])
0
"""
vowels = {'a', 'e', 'i', 'o', 'u'}
return sum(1 for w in words
if w[0].casefold() in vowels or w[-1].casefold() in vowels)
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
Trying:
word_count("unicode", "xml", "raku", "perl")
Expecting:
2
ok
Trying:
word_count("the", "weekly", "challenge")
Expecting:
2
ok
Trying:
word_count("perl", "python", "postgres")
Expecting:
0
ok
1 items had no tests:
__main__
1 items passed all tests:
3 tests in __main__.word_count
3 tests in 2 items.
3 passed and 0 failed.
Test passed.
Task 2: Minimum Common
This is my function to answer task 2. It relys on the fact min raises a ValueError if the iterable passed to it is empty and there is no default provided[1].
#!/usr/bin/env python3
def minimum_common(one, two):
""" Given two arrays of integers, return the minimum integer common to both
arrays. If none found return -1.
>>> minimum_common((1, 2, 3, 4), (3, 4, 5, 6))
3
>>> minimum_common((1, 2, 3), (2, 4))
2
>>> minimum_common((1, 2, 3, 4), (5, 6, 7, 8))
-1
"""
try:
return min(set(one).intersection(two))
except ValueError:
return -1
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
Trying:
minimum_common((1, 2, 3, 4), (3, 4, 5, 6))
Expecting:
3
ok
Trying:
minimum_common((1, 2, 3), (2, 4))
Expecting:
2
ok
Trying:
minimum_common((1, 2, 3, 4), (5, 6, 7, 8))
Expecting:
-1
ok
1 items had no tests:
__main__
1 items passed all tests:
3 tests in __main__.minimum_common
3 tests in 2 items.
3 passed and 0 failed.
Test passed.