Skip to content

Commit

Permalink
Merge pull request #152 from MIT-Emerging-Talent/count-character
Browse files Browse the repository at this point in the history
add solution to Count Character challenge and updated README
  • Loading branch information
Bikaze authored Jan 13, 2025
2 parents ac2770e + 8735ea5 commit cedd98b
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions solutions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ while corresponding test files are maintained in the `tests` folder.
| `check_prime_number.py` | Given a positive int if it is a prime number| Özgür |
| `password_strength.py` | Checks the strength of a password| Anas |
| `decimal_to_binary.py` | Converts decimal to its equivalent binary| Anas |
| `count_character.py`| counts occurrences of a character in a string| Mohamed|
| `is_positive.py` | Determines if a given number is positive | Faisal |
| `is_palindrome.py` | Checks string palindrome properties | Faisal |

Expand Down
49 changes: 49 additions & 0 deletions solutions/count_character.py
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)
56 changes: 56 additions & 0 deletions solutions/tests/test_count_character.py
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")

0 comments on commit cedd98b

Please sign in to comment.