Skip to content

Commit

Permalink
count vowels challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
danielluyi committed Jan 12, 2025
1 parent 48565fa commit 1d222be
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
46 changes: 46 additions & 0 deletions solutions/count_vowels.py
Original file line number Diff line number Diff line change
@@ -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)
53 changes: 53 additions & 0 deletions solutions/tests/test_count_vowels.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 1d222be

Please sign in to comment.