From 3ed590a40095df583b1e381977f21ff000d60a96 Mon Sep 17 00:00:00 2001 From: Hopeeid Date: Tue, 7 Jan 2025 23:14:39 +0100 Subject: [PATCH 1/4] wrote a function and its tests to count specific letters in a string --- solutions/count_specific_string_letters.py | 47 +++++++++ .../test_count_specific_string_letters.py | 95 +++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 solutions/count_specific_string_letters.py create mode 100644 solutions/tests/test_count_specific_string_letters.py diff --git a/solutions/count_specific_string_letters.py b/solutions/count_specific_string_letters.py new file mode 100644 index 000000000..6807a3815 --- /dev/null +++ b/solutions/count_specific_string_letters.py @@ -0,0 +1,47 @@ +""" +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: + AssertionError: if the argument(letters) is not a string + AssertionError: if the argument(sentence) is not a string + 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' + + + """ + 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_specific_string_letters.py b/solutions/tests/test_count_specific_string_letters.py new file mode 100644 index 000000000..41f93714c --- /dev/null +++ b/solutions/tests/test_count_specific_string_letters.py @@ -0,0 +1,95 @@ +import unittest +from solutions.count_specific_string_letters 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() + + + + From 9495a77d5ae67cca1bebdb59667115beb36c3312 Mon Sep 17 00:00:00 2001 From: Hopeeid Date: Wed, 8 Jan 2025 01:50:01 +0100 Subject: [PATCH 2/4] ensured python formatting checks --- solutions/count_specific_string_letters.py | 15 ++++++--------- .../tests/test_count_specific_string_letters.py | 13 +++++++------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/solutions/count_specific_string_letters.py b/solutions/count_specific_string_letters.py index 6807a3815..f4254b489 100644 --- a/solutions/count_specific_string_letters.py +++ b/solutions/count_specific_string_letters.py @@ -1,4 +1,4 @@ -""" +""" A function that counts a specific string letters in a sentence using the count method. function contents - @@ -10,7 +10,8 @@ """ -def count_string(sentence:str, letters:str) -> str: + +def count_string(sentence: str, letters: str) -> str: """ This function counts the occurrence of a specific letter in a given sentence. @@ -37,11 +38,7 @@ def count_string(sentence:str, letters:str) -> str: """ 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") - - - + 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_specific_string_letters.py b/solutions/tests/test_count_specific_string_letters.py index 41f93714c..b71adb6a0 100644 --- a/solutions/tests/test_count_specific_string_letters.py +++ b/solutions/tests/test_count_specific_string_letters.py @@ -14,12 +14,13 @@ Author: Hope Udoh """ + class TestCountString(unittest.TestCase): """ Test suite for the count_string function. """ - #Standard Cases + # Standard Cases def test_single_occurrence(self): """ Standard Case: Test that the function correctly counts a single occurrence of a letter. @@ -86,10 +87,10 @@ def test_large_input(self): >>> 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() + self.assertEqual( + count_string("a" * 10000, "a"), "The letter 'a' appears 10000 times" + ) - - +if __name__ == "__main__": + unittest.main() From 801912114e451067341c74dab81e7310f86e7089 Mon Sep 17 00:00:00 2001 From: Hopeeid Date: Sat, 11 Jan 2025 17:16:04 +0100 Subject: [PATCH 3/4] resolved review issues --- solutions/count_specific_string_letters.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/solutions/count_specific_string_letters.py b/solutions/count_specific_string_letters.py index f4254b489..06279584d 100644 --- a/solutions/count_specific_string_letters.py +++ b/solutions/count_specific_string_letters.py @@ -23,8 +23,6 @@ def count_string(sentence: str, letters: str) -> str: str: A string indicating the count of the specific letter in the sentence. Raises: - AssertionError: if the argument(letters) is not a string - AssertionError: if the argument(sentence) is not a string ValueError: If the letters parameter is an empty string. Example 1: @@ -34,7 +32,10 @@ def count_string(sentence: str, letters: str) -> str: 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: @@ -42,3 +43,4 @@ def count_string(sentence: str, letters: str) -> str: specific_letter_count = sentence.count(letters) return f"The letter '{letters}' appears {specific_letter_count} times" + From fb6a76e15efcb85176323f13fc649497a4c7535e Mon Sep 17 00:00:00 2001 From: Hopeeid Date: Sat, 11 Jan 2025 20:35:00 +0100 Subject: [PATCH 4/4] renamed file name to match naming convention --- .../{count_specific_string_letters.py => count_string.py} | 3 +-- ...t_count_specific_string_letters.py => test_count_string.py} | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) rename solutions/{count_specific_string_letters.py => count_string.py} (99%) rename solutions/tests/{test_count_specific_string_letters.py => test_count_string.py} (97%) diff --git a/solutions/count_specific_string_letters.py b/solutions/count_string.py similarity index 99% rename from solutions/count_specific_string_letters.py rename to solutions/count_string.py index 06279584d..2a2b69cd3 100644 --- a/solutions/count_specific_string_letters.py +++ b/solutions/count_string.py @@ -32,7 +32,7 @@ def count_string(sentence: str, letters: str) -> str: 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' @@ -43,4 +43,3 @@ def count_string(sentence: str, letters: str) -> str: specific_letter_count = sentence.count(letters) return f"The letter '{letters}' appears {specific_letter_count} times" - diff --git a/solutions/tests/test_count_specific_string_letters.py b/solutions/tests/test_count_string.py similarity index 97% rename from solutions/tests/test_count_specific_string_letters.py rename to solutions/tests/test_count_string.py index b71adb6a0..5df597582 100644 --- a/solutions/tests/test_count_specific_string_letters.py +++ b/solutions/tests/test_count_string.py @@ -1,5 +1,5 @@ import unittest -from solutions.count_specific_string_letters import count_string +from solutions.count_string import count_string """