From 7f428d51fe55a457c1341f64ae2eda46801b4114 Mon Sep 17 00:00:00 2001 From: lolabird Date: Sun, 19 Nov 2023 18:25:49 -0700 Subject: [PATCH] Add solution to problem 4 --- thinkcspy/Chapter-12-Exercises/Problem4.py | 35 +++++++++++++++++++ .../alice_words_by_number.txt | 0 2 files changed, 35 insertions(+) create mode 100644 thinkcspy/Chapter-12-Exercises/Problem4.py create mode 100644 thinkcspy/Chapter-12-Exercises/alice_words_by_number.txt diff --git a/thinkcspy/Chapter-12-Exercises/Problem4.py b/thinkcspy/Chapter-12-Exercises/Problem4.py new file mode 100644 index 0000000..09b4879 --- /dev/null +++ b/thinkcspy/Chapter-12-Exercises/Problem4.py @@ -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() diff --git a/thinkcspy/Chapter-12-Exercises/alice_words_by_number.txt b/thinkcspy/Chapter-12-Exercises/alice_words_by_number.txt new file mode 100644 index 0000000..e69de29