Skip to content

Commit

Permalink
wrote a function and its tests to count specific letters in a string
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopeeid committed Jan 7, 2025
1 parent 6156dd5 commit 3ed590a
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
47 changes: 47 additions & 0 deletions solutions/count_specific_string_letters.py
Original file line number Diff line number Diff line change
@@ -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")




95 changes: 95 additions & 0 deletions solutions/tests/test_count_specific_string_letters.py
Original file line number Diff line number Diff line change
@@ -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()




0 comments on commit 3ed590a

Please sign in to comment.