Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Challenge/count_string_letters #48

Merged
merged 4 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions solutions/count_string.py
Original file line number Diff line number Diff line change
@@ -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"
96 changes: 96 additions & 0 deletions solutions/tests/test_count_string.py
Original file line number Diff line number Diff line change
@@ -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()
Loading