forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from MIT-Emerging-Talent/count-character
add solution to Count Character challenge and updated README
- Loading branch information
Showing
3 changed files
with
106 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
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,49 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for counting occurrences of a character in a string. | ||
Created on 12/1/2025 | ||
@author: Mohamed Altayeb | ||
Group: ET foundations group 16 (Matrix) | ||
Module functions: | ||
- count_character: counts occurrences of a character in a string. | ||
""" | ||
|
||
|
||
def count_character(input_string: str, character: str) -> int: | ||
""" | ||
Counts the number of occurrences of a specific character in a string | ||
It will treat capital letters as different characters compared to small letters. | ||
Arguments: | ||
input_string (str): The string to count in. | ||
character (str): The character to count. | ||
Returns: | ||
int: The number of occurrences of the character in the string. | ||
Raises: | ||
AssertionError: if input string is not a string or character is not a single | ||
character string. | ||
>>> count_character("Mohamed", "m") | ||
1 | ||
>>> count_character("test", "l") | ||
0 | ||
>>> count_character("mississippi", "s") | ||
4 | ||
""" | ||
# Make sure inputs are correct | ||
if not isinstance(input_string, str): | ||
raise AssertionError("The input_string must be a string.") | ||
if not isinstance(character, str) or len(character) != 1: | ||
raise AssertionError("The character must be a single character string.") | ||
|
||
# Return the number of occurrences of character in the string | ||
return input_string.count(character) |
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,56 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Test module for count_character function | ||
Created on 11/1/2025 | ||
@author: Mohamed Altayeb | ||
Group: ET foundations group 16 (Matrix) | ||
""" | ||
|
||
import unittest | ||
|
||
from ..count_character import count_character | ||
|
||
|
||
class TestCountCharacter(unittest.TestCase): | ||
"""Test suite for the count_character function""" | ||
|
||
def test_string_with_no_occurrence(self): | ||
"""it should return zero when the character is not in the string""" | ||
self.assertEqual(count_character("abcdefg", "h"), 0) | ||
|
||
def test_empty_string(self): | ||
"""it should return zero when the input string is empty""" | ||
self.assertEqual(count_character("", "h"), 0) | ||
|
||
def test_string_with_normal_occurrence(self): | ||
"""it should return the number of occurrences when the character in the string""" | ||
self.assertEqual(count_character("looking for a char", "o"), 3) | ||
|
||
def test_string_with_capital_and_small_letters(self): | ||
"""it should treat capital letters as different characters compared to their small | ||
counterparts""" | ||
self.assertEqual(count_character("Mohammed Made soMething", "M"), 3) | ||
|
||
def test_counting_spaces(self): | ||
"""it should count the number of spaces in a string when passed a space as | ||
a character""" | ||
self.assertEqual( | ||
count_character("there are a l ot of spaces i n this l ine", " "), 13 | ||
) | ||
|
||
def test_input1_is_not_a_string(self): | ||
"""It should raise AssertionError if first input is not a string""" | ||
with self.assertRaises(AssertionError): | ||
count_character(["mohamed"], "m") | ||
|
||
def test_character_is_not_a_string(self): | ||
"""It should raise AssertionError if first input is not a single character string""" | ||
with self.assertRaises(AssertionError): | ||
count_character(["mohamed"], 2) | ||
|
||
def test_length_of_character_is_not_one(self): | ||
"""It should raise AssertionError if length of character is not one""" | ||
with self.assertRaises(AssertionError): | ||
count_character(["mohamed"], "med") |