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

A function called freq_distribution with two parameters: infile and distfile. Th

ID: 3671936 • Letter: A

Question

A function called freq_distribution with two parameters: infile and distfile. The first parameter, infile, is the name of the file for which we want to compute the frequency distribution. The second parameter, distfile is the name of the file into which the frequency distribution is to be written in sorted order of the words. That is, the words should be listed in alphabetically sorted order in the output file. Each line of the file should contain the word and its frequency. Use string formatting to make sure the output is neatly formatted.

In order to carry out the above task, you must first create a dictionary to store the frequency distribution. You will do this by calling the helper function freq dictionary described in #(3) below. Remember to close all files used in the function

This is the program used for this problem. Please use correct indentation and this is to be written in python 3.5.

For example, suppose poem.txt is the name of a file containing the following poem (courtesy of Dr. Seuss):

You have brains in your head.

You have feet in your shoes.

You can steer yourself in

any direction you choose.

Then, after the function call freq_distribution("poem.txt", "freq.txt"), a file called freq.txt should contain the following:

any 1

brains 1

can 1

choose 1

direction 1

feet 1

have 2

head 1

in 3

shoes 1

steer 1

you 4

your 2

yourself 1

Explanation / Answer

from collections import Counter
with open('data.txt', 'r') as f:
data = f.readlines()
myList = []
for line in data:
words = line.split()
for s in words:
myList.append(s)
myList.sort()
print "after sorting"
print myList
cnt = Counter(myList)
print cnt
with open('freq.txt','w') as f1:
for k,v in cnt.most_common():
f1.write( "{} {} ".format(k,v) )
print k
print v

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote