Hey everyone! π
Today, we're tackling a string manipulation problem: Counting Character Frquency.
The Problem
The goal is to write a function that returns a dictionary with the frequency of each character in a string. The function should be case-insensitive and ignore spaces.
Example:
-
char_frequency("hello world")should return{'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1}
The Solution
Here is the Python implementation:
def char_frequency(s):
"""
Returns a dictionary with the frequency of each character in a string.
Ignores spaces and converts to lowercase.
"""
freq_dict = {}
s = s.lower().replace(" ", "")
for char in s:
if char in freq_dict:
freq_dict[char] += 1
else:
freq_dict[char] = 1
return freq_dict
# Test case
print(char_frequency("hello world"))
# Output: {'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1}
Code Breakdown
Let's walk through the code line by line:
-
def char_frequency(s):- Defines a function named
char_frequencythat takes one parameters(a string).
- Defines a function named
-
freq_dict = {}- Creates an empty dictionary called
freq_dictto store the characters and their counts.
- Creates an empty dictionary called
-
s = s.lower().replace(" ", "")- Converts the input string
sto lowercase using.lower()to ensure case-insensitivity. - Removes all spaces from the string using
.replace(" ", "")so they aren't counted.
- Converts the input string
-
for char in s:- Starts a loop that iterates through each character in the processed string
s.
- Starts a loop that iterates through each character in the processed string
-
if char in freq_dict:- Checks if the current character
charis already a key in thefreq_dict.
- Checks if the current character
-
freq_dict[char] += 1- If the character is already in the dictionary, increments its count by 1.
-
else: freq_dict[char] = 1- If the character is not in the dictionary (it's the first time we've seen it), adds it to the dictionary with a count of 1.
-
return freq_dict- Returns the final dictionary containing the character frequencies.
Example Walkthrough with "hello world"
-
Preprocessing:
- Input:
"hello world" - Lowercase & Remove Spaces:
"helloworld"
- Input:
-
Iteration:
-
char = 'h': Not in dict βfreq_dict = {'h': 1} -
char = 'e': Not in dict βfreq_dict = {'h': 1, 'e': 1} -
char = 'l': Not in dict βfreq_dict = {'h': 1, 'e': 1, 'l': 1} -
char = 'l': In dict βfreq_dict = {'h': 1, 'e': 1, 'l': 2} -
char = 'o': Not in dict βfreq_dict = {'h': 1, 'e': 1, 'l': 2, 'o': 1} -
char = 'w': Not in dict βfreq_dict = {'h': 1, 'e': 1, 'l': 2, 'o': 1, 'w': 1} -
char = 'o': In dict βfreq_dict = {'h': 1, 'e': 1, 'l': 2, 'o': 2, 'w': 1} -
char = 'r': Not in dict βfreq_dict = {'h': 1, 'e': 1, 'l': 2, 'o': 2, 'w': 1, 'r': 1} -
char = 'l': In dict βfreq_dict = {'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1} -
char = 'd': Not in dict βfreq_dict = {'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1}
-
Final result: {'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1}
Happy coding! π»
Top comments (0)