Lab 12: Compute the most frequently used words from the text file
Problem statement
sample input 0: the peacock is the beautiful bird...
sample output 0: the
sample input 1: sea shells is in the sea shore
sample output 1: sea
sample input 2: It is a quite easy program
sample output 2: There are no frequently used wordsSolution Key
def word_freq():
max = 0
val = ''
counter = {}
f = open('animal.txt', 'r')
contents = f.read()
words = contents.split()
for i in range(len(words)):
words[i] = words[i].strip(',:.;')
if words[i].lower() not in counter:
counter[words[i].lower()] = 1
else:
counter[words[i].lower()] += 1
for i in counter.keys():
if counter[i] > max:
max = counter[i]
val = i
return valCloudCoder Exercise
Pre Lab Questions
Post Lab Questions
Bonus 1
Related Material
PreviousLab 11: Programs that take command line argumentsNextLab 12: Find the most frequent words in a text read from a file
Last updated