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
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
I made it as part of the weekly challenges for the SlothBytes Newsletter (Go check them out!)
I enjoy Python's readability, it's the only language where the code feels so satisfying for me so here's a little script while I kept in mind all of the best practices and conventions I learned throughout the years.
Notably:
blackfor formatting code.if __name__ == "__main__"unittest(You don't even need to install any testing frameworks so no excuses not to test)