-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path12_mostcommoncharacter3.py
More file actions
34 lines (25 loc) · 1013 Bytes
/
Copy path12_mostcommoncharacter3.py
File metadata and controls
34 lines (25 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# We can improve this code. What if there are two characters
# that occur the same number of times? We would certainly want
# to know this:
myParagraph = "Hi, my name is Paul Vierthaler"
# Create a variable to store the most common character. Only
# this time, use a list:
mostCommonCharacter = []
mCCFreq = 0
for char in myParagraph:
if char != " ":
freq = myParagraph.count(char)
if freq > mCCFreq:
mCCFreq = freq
# Note that now we are saving the character inside
# a list!
mostCommonCharacter = [char]
# If freq is equal to the current value then save the
# other character too!
elif freq == mCCFreq:
mostCommonCharacter.append(char)
# Let's print the values each time to track what is
# happening:
print(mostCommonCharacter, mCCFreq)
# Print the final results
print(f"The most common character(s) is/are {mostCommonCharacter}, which occur(s) {mCCFreq} time(s)!")