diff --git a/solutions/count_string.py b/solutions/count_string.py new file mode 100644 index 000000000..2a2b69cd3 --- /dev/null +++ b/solutions/count_string.py @@ -0,0 +1,45 @@ +""" +A function that counts a specific string letters in a sentence using the count method. + +function contents - + count_string = a function that counts the occurrence of a specific letter in a given sentence or phrase. + + created on 07 Jan 2025 + @author: Hope Udoh + + +""" + + +def count_string(sentence: str, letters: str) -> str: + """ + This function counts the occurrence of a specific letter in a given sentence. + + Parameters: + sentence (str): The input sentence where the letter count will be performed. + letters (str): The specific letter to count its occurrence in the sentence. + + Returns: + str: A string indicating the count of the specific letter in the sentence. + + Raises: + ValueError: If the letters parameter is an empty string. + + Example 1: + >>> count_string("hello", "l") + 'the letter 'l' appears 2 times' + + Example 2: + >>> count_string("I love pineapples too much", "t")) + 'the letter 't' appears 1 times' + + Example 3: + >>> count_string("Hope", "x")) + 'the letter 'x' appears 0 times' + + """ + if not letters: + raise ValueError("The letter to count must not be empty.") + + specific_letter_count = sentence.count(letters) + return f"The letter '{letters}' appears {specific_letter_count} times" diff --git a/solutions/tests/test_count_string.py b/solutions/tests/test_count_string.py new file mode 100644 index 000000000..5df597582 --- /dev/null +++ b/solutions/tests/test_count_string.py @@ -0,0 +1,96 @@ +import unittest +from solutions.count_string import count_string + + +""" +Test module for the count_string function. +This file includes unittest to verify the correctness of the count_string function. +It tests the function with standard cases and edge cases. +Test categories: + - Standard cases: Test that the function correctly counts a single occurrence of a letter. + - Edge cases: empty sentence and empty letter + - Large input: lists with a large range of strings (e.g., thousands) +Created on: 07 January 2025 +Author: Hope Udoh +""" + + +class TestCountString(unittest.TestCase): + """ + Test suite for the count_string function. + """ + + # Standard Cases + def test_single_occurrence(self): + """ + Standard Case: Test that the function correctly counts a single occurrence of a letter. + + Example: + >>> count_string("hello", "e") + 'The letter 'e' appears 1 times' + """ + self.assertEqual(count_string("hello", "e"), "The letter 'e' appears 1 times") + + def test_multiple_occurrences(self): + """ + Standard Case: Test that the function correctly counts multiple occurrences of a letter. + + Example: + >>> count_string("banana", "a") + 'The letter 'a' appears 3 times' + """ + self.assertEqual(count_string("banana", "a"), "The letter 'a' appears 3 times") + + def test_no_occurrence(self): + """ + Standard Case: Test that the function returns zero when the letter does not occur. + + Example: + >>> count_string("world", "x") + 'The letter 'x' appears 0 times' + """ + self.assertEqual(count_string("world", "x"), "The letter 'x' appears 0 times") + + # Edge Cases + def test_empty_sentence(self): + """ + Edge Case: Test that the function returns zero when the sentence is empty. + + Example: + >>> count_string("", "a") + 'The letter 'a' appears 0 times' + """ + self.assertEqual(count_string("", "a"), "The letter 'a' appears 0 times") + + def test_empty_letter(self): + """ + Edge Case: Test that the function raises a ValueError when the letter is empty. + + Raises: + ValueError: If the letter is an empty string. + + Example: + >>> count_string("hello", "") + Traceback (most recent call last): + ... + ValueError: The letter to count must not be empty. + """ + with self.assertRaises(ValueError): + count_string("hello", "") + + # Large Input Cases + def test_large_input(self): + """ + Large Input Case: Test the function with a large input sentence. + + Example: + >>> count_string("a" * 10000, "a") + 'The letter 'a' appears 10000 times' + """ + self.assertEqual( + count_string("a" * 10000, "a"), "The letter 'a' appears 10000 times" + ) + + +if __name__ == "__main__": + unittest.main()