107k views
2 votes
g Define a function called most_frequent_letter which takes no arguments. This function will return a dictionary which will have as keys the words in the file words.txt and values the most frequent letter occurring in the word. So for the word ’hello’ the value will be ’l’.

1 Answer

6 votes

Answer:

def most_frequent_letter():

file = open("words","r")

dWords = {}

for line in file:

line = line.rstrip()

words = line.split(" ")

for word in words:

counts = {}

for c in word:

counts[c] = counts.get(c, 0) + 1

dWords[word] = max(counts, key=counts.get)

return dWords

print(most_frequent_letter())

Step-by-step explanation:

the file used was words in txt format and its contents are as follows:

hello aamir jan khan

parallelogram abdullah

anaconda ali

pycharm notebook

User Hans Espen
by
5.2k points