diff --git a/solutions/count_vowels.py b/solutions/count_vowels.py new file mode 100644 index 000000000..61c19e7da --- /dev/null +++ b/solutions/count_vowels.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +A module for counting the number of vowels in a given string. + +Created on 29 Dec 2024 +@author: Daniel Oluwaluyi +""" + + +def count_vowels(input_string: str) -> int: + """ + Count the number of vowels in the provided string. + + This function takes a string as input and counts the occurrences + of vowels (a, e, i, o, u), including both lowercase and uppercase letters. + + Args: + input_string (str): The input string to process. + + Returns: + int: The count of vowels in the input string. + + Raises: + AssertionError: If the input is not a string. + + Doctests: + >>> count_vowels("hello") + 2 + >>> count_vowels("HELLO") + 2 + >>> count_vowels("12345!@#$%^&*()") + 0 + >>> count_vowels("aeiouAEIOU") + 10 + >>> count_vowels("") + 0 + """ + # Defensive assertion to ensure the input is a string + assert isinstance(input_string, str), "Input must be a string" + + # Define a set of vowels for quick lookup + vowels = set("aeiouAEIOU") + + # Count and return the number of vowels in the string + return sum(1 for char in input_string if char in vowels) diff --git a/solutions/tests/test_count_vowels.py b/solutions/tests/test_count_vowels.py new file mode 100644 index 000000000..51b5d0507 --- /dev/null +++ b/solutions/tests/test_count_vowels.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +A module for testing the count_vowels function. + +Created on 29 Dec 2024 +@author: Daniel Oluwaluyi +""" + +import unittest + +from solutions.count_vowels import count_vowels + + +class TestCountVowels(unittest.TestCase): + """A class for testing the count_vowels function.""" + + def test_empty_string(self): + """Test the count_vowels function with an empty string.""" + self.assertEqual(count_vowels(""), 0) + + def test_no_vowels(self): + """Test the count_vowels function with a string containing no vowels.""" + self.assertEqual(count_vowels("bcdfghjklmnpqrstvwxyz"), 0) + + def test_all_vowels(self): + """Test the count_vowels function with a string containing all vowels.""" + self.assertEqual(count_vowels("aeiouAEIOU"), 10) + + def test_mixed_string(self): + """Test the count_vowels function with a string containing vowels and consonants.""" + self.assertEqual(count_vowels("Hello, World!"), 3) + + def test_numeric_and_special_characters(self): + """Test the count_vowels function with a string containing numbers and special characters.""" + self.assertEqual(count_vowels("12345!@#$%^&*()"), 0) + + def test_vowels_in_words(self): + """Test the count_vowels function with a string containing multiple words.""" + self.assertEqual(count_vowels("Python programming is fun!"), 6) + + def test_string_with_whitespace(self): + """Test the count_vowels function with a string containing leading and trailing whitespace.""" + self.assertEqual(count_vowels(" aeiou "), 5) + + def test_non_string_input(self): + """Test the count_vowels function with a non-string input.""" + with self.assertRaises(AssertionError): + count_vowels(12345) + + +if __name__ == "__main__": + unittest.main()