Skip to content

Instantly share code, notes, and snippets.

@ravener
Last active June 18, 2024 15:49
Show Gist options
  • Select an option

  • Save ravener/90064eae5a4d754c3d31582287c6901b to your computer and use it in GitHub Desktop.

Select an option

Save ravener/90064eae5a4d754c3d31582287c6901b to your computer and use it in GitHub Desktop.
Count the number of vowels in a word
#!/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()
@ravener

ravener commented Jun 18, 2024

Copy link
Copy Markdown
Author

Update: I made a dedicated repository to host my solutions and to continue doing the challenges which you can find here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment