forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48565fa
commit 1d222be
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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) |
This file contains 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
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() |