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 12, 2024

Copy link
Copy Markdown
Author

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:

  • Using type annotations
  • Using list comprehensions
  • Docstrings
  • Using black for formatting code.
  • Using if __name__ == "__main__"
  • Unit testing with the standard library's unittest (You don't even need to install any testing frameworks so no excuses not to test)

@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