GROK-Complex data structur Introduction to Programming 2 Python) LEARNIN D Probl
ID: 3709616 • Letter: G
Question
GROK-Complex data structur Introduction to Programming 2 Python) LEARNIN D Problem appears and print out the top 3 words which start with a capital letter. For example, for our first sample file which you can download here Jellicle Cats are black and white, Jellicle Cats are rather small; Jellicle Cats are merry and bright, And pleasant to hear when they caterwaut. Jellicle Cats have cheer ful faces, Jellicle Cats have bright black eyes; They like to practise their airs and graces And wait for the Jellicle Moon to rise. Your program should print out 6 Jellicle 5 Cats 2 And 6 because the word Jellicle is the most frequently Capitalised word (occurring 6 times), followed by Cats and then And. Once you've got it working on that simple example, lets try something really ambitious and run it on a novel Pride and Prejudice. You can download a large chunk of text here. Your program should output 899 I 521 Mr 210 E11zabeth You can experiment on other novels freely available from Project Gutenberg O Type here to search ??Explanation / Answer
def frequent_words(filename): d = {} with open(filename, 'r') as f: for line in f: words = line.strip().split() for word in words: if word[0].isupper(): if word not in d: d[word] = 0 d[word] += 1 word_counts = [(count, word) for word, count in d.items()] word_counts.sort() word_counts.reverse() i = 0 for count, word in word_counts: if i < 3: print(count, word) i += 1 frequent_words('input.txt')Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.