-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path12_mostcommoncharacter4.py
More file actions
32 lines (21 loc) · 899 Bytes
/
Copy path12_mostcommoncharacter4.py
File metadata and controls
32 lines (21 loc) · 899 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
# Note that last time we got a list of with many duplicates!
# Let's only save the character if we are not already tracking
# it!
myParagraph = myParagraph = "Hi, my name is Paul Vierthaler"
mostCommonCharacter = []
mCCFreq = 0
for char in myParagraph:
if char != " ":
freq = myParagraph.count(char)
if freq > mCCFreq:
mCCFreq = freq
mostCommonCharacter = [char]
# Save if freq is equal to the current value and char
# not already in the list!
elif freq == mCCFreq and char not in mostCommonCharacter:
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)!")