Last active
June 18, 2024 15:49
-
-
Save ravener/90064eae5a4d754c3d31582287c6901b to your computer and use it in GitHub Desktop.
Count the number of vowels in a word
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf8 -*- | |
| import unittest | |
| vowels: list[str] = ["a", "e", "i", "o", "u"] | |
| def count_vowels(word: str) -> int: | |
| """ | |
| Counts the vowels in a given word. | |
| :param word: The word to count from. | |
| :returns: How many vowels were found. | |
| """ | |
| return len(list(letter for letter in word if letter.lower() in vowels)) | |
| class TestCountVowels(unittest.TestCase): | |
| def test_words(self) -> None: | |
| self.assertEqual(count_vowels("Celebration"), 5) | |
| self.assertEqual(count_vowels("Palm"), 1) | |
| self.assertEqual(count_vowels("Prediction"), 4) | |
| if __name__ == "__main__": | |
| unittest.main() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update: I made a dedicated repository to host my solutions and to continue doing the challenges which you can find here