CodeWars: Vowel Count
Table of Contents
The Problem
Given a string, count the number of vowels in it. The vowels are "aeiou" and the letters will be lower-cased.
The Solution
The Tests
# pypi
from expects import equal, expect
expect(vowel_count("a")).to(equal(1))
expect(vowel_count("rmnl")).to(equal(0))
expect(vowel_count("a mouse is not a house")).to(equal(10))
The Function
VOWELS = set("aeiou")
def vowel_count(letters: str) -> int:
"""Counts the number of vowels in the input
Args:
letters: lower-cased string to check for vowels
Returns:
count of vowels in the letters
"""
return sum(1 for letter in letters if letter in VOWELS)
Alternatives
One solution used regular expressions and the findall
method. This seems better in a generalizable sense, but I think that the findall
will build a list rather than a generator so might not be as efficient space-wise, and is probably slower. Others used the python string method - count
. I think this problem is so easy that there's really not a lot of stuff you can do that doesn't overcomplicate things.
Anyway, day one.
End
- CodeWars Vowel Count: One of the beginner's kata.