Add solution to problem 4
This commit is contained in:
parent
4111e11283
commit
7f428d51fe
2 changed files with 35 additions and 0 deletions
35
thinkcspy/Chapter-12-Exercises/Problem4.py
Normal file
35
thinkcspy/Chapter-12-Exercises/Problem4.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
aliceBook = open("alice_in_wonderland.txt", "r")
|
||||
aliceDict = {}
|
||||
|
||||
for line in aliceBook:
|
||||
words = line.split()
|
||||
|
||||
for word in words:
|
||||
word = word.lower()
|
||||
newWord = ""
|
||||
|
||||
for char in word:
|
||||
if char.isalpha():
|
||||
newWord += char
|
||||
|
||||
if newWord != "":
|
||||
word = newWord
|
||||
|
||||
if word not in aliceDict:
|
||||
aliceDict[word] = 1
|
||||
else:
|
||||
aliceDict[word] += 1
|
||||
|
||||
aliceList = list(aliceDict.items())
|
||||
|
||||
maxnum = 0
|
||||
maxWord = ""
|
||||
|
||||
for item in aliceList:
|
||||
if len(item[0]) > maxnum:
|
||||
maxnum = len(item[0])
|
||||
maxWord = item[0]
|
||||
|
||||
print(maxWord, "-", maxnum)
|
||||
|
||||
aliceBook.close()
|
||||
0
thinkcspy/Chapter-12-Exercises/alice_words_by_number.txt
Normal file
0
thinkcspy/Chapter-12-Exercises/alice_words_by_number.txt
Normal file
Loading…
Reference in a new issue