Skip to content

Commit 8860f4a

Browse files
authored
Create unit8_ex8.3.3.py
1 parent 2c52b82 commit 8860f4a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

unit8_ex8.3.3.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# exercise 8.3.3 from unit 8
2+
'''
3+
Write a function called count_chars defined like this:
4+
5+
def count_chars(my_str):
6+
The function accepts a string as a parameter.
7+
The function returns a dictionary, so that each element in it is a pair consisting of a key: a character from the passed string (not including spaces), and an array: the number of times the character appears in the string.
8+
9+
An example of running the count_chars function:
10+
>>> magic_str = "abra cadabra"
11+
>>> count_chars(magic_str)
12+
{'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
13+
'''
14+
15+
def main():
16+
def count_chars_helper(my_str):
17+
char_counts = {}
18+
for char in my_str:
19+
if char == ' ':
20+
continue
21+
if char not in char_counts:
22+
char_counts[char] = 1
23+
else:
24+
char_counts[char] += 1
25+
return char_counts
26+
27+
magic_str = "abra cadabra"
28+
print(count_chars_helper(magic_str))
29+
30+
main()
31+

0 commit comments

Comments
 (0)