Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(b) Write a function with header def computeWordFrequencies(filename): that take

ID: 643913 • Letter: #

Question

(b) Write a function with header def computeWordFrequencies(filename): that takes a string parameter called filename and reads from a file of that name and returns a list, say L, consisting of two lists. L[0] is required to be the list of all distinct, lowercase words of length at least 4 in the given file and L[1] is required to be the corresponding list of frequencies of these words. For example, if the function returns [["hello", "this", "ball", "bombastic"], [3, 7, 1, 5]] this means that in the file that was read by the function, the word

Explanation / Answer

def computeWordFrequencies(filename): #my function f = open(filename,'r') j = f.read() OriginalL1 = parse(j) L1 = unique(parse(j)) L2 = [OriginalL1.count(freq) for freq in L1] L = [L1, L2] return L def mostFrequentWords(word,frequency,n): words = word freqs = sorted(frequency,reverse=True) return freqs[:n] L = computeWordFrequencies('file.txt') #takes a file and returns words & their frequencies words = zip(*sorted(zip(L[0],L[1]))) freqs = L[1] print mostFrequentWords(words,freqs,100)