-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path12_mostcommoncharacter1.py
More file actions
27 lines (20 loc) · 1.54 KB
/
12_mostcommoncharacter1.py
File metadata and controls
27 lines (20 loc) · 1.54 KB
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
# Now we can build a program that actually does something!
# Let's calculate the most common character in this paragraph:
myParagraph = "In the year 1878 I took my degree of Doctor of Medicine of the University of London, and proceeded to Netley to go through the course prescribed for surgeons in the army. Having completed my studies there, I was duly attached to the Fifth Northumberland Fusiliers as Assistant Surgeon. The regiment was stationed in India at the time, and before I could join it, the second Afghan war had broken out. On landing at Bombay, I learned that my corps had advanced through the passes, and was already deep in the enemy’s country. I followed, however, with many other officers who were in the same situation as myself, and succeeded in reaching Candahar in safety, where I found my regiment, and at once entered upon my new duties."
print(myParagraph)
# Create a variable to store the most common character
mostCommonCharacter = ""
# Create a variable to track how often the character occurs
mCCFreq = 0
# Go through each character and count how often it occurs
for char in myParagraph:
# Remember, we saw the count method earlier!
freq = myParagraph.count(char)
# if freq is greater than the current value stored in
# mCCFreq, it is the most common character seen so far!
# Save both the frequency and the character
if freq > mCCFreq:
mCCFreq = freq
mostCommonCharacter = char
# Print the results:
print(f"The most common character is {mostCommonCharacter}, which occurs {mCCFreq} times!")