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

A function called ordered freq distribution with two parameters: infile and orde

ID: 3562705 • Letter: A

Question

A function called ordered freq distribution with two parameters: infile and ordered distfile. The first parameter, infile, is the name of the file for which we want to compute the frequency distribution. The second parameter, ordered distfile is the name of the file into which the frequency distribution is to be written in sorted order of the frequency. That is, the characters should be printed into the file in decreas- ing order of frequency. If several characters have the same frequency, they should be printed in alphabetical order. Use string formatting to make sure that the output in ordered distfile is neatly formatted. Once again, in order to carry out this task, you must first create a dictionary to store the frequency distribution by calling the helper function freq dictionary described in #(3) below. Remember to close ordered distfile when you are done writing into it.

Explanation / Answer

def freq_dictionary(infile ,distfile):
inf = open(infile, 'r')
indata = inf.read()
dict = {}
for i in file:
if i != '':
dict[i] = dict.get(i,0)+1
file.close()
re = lambda (a1, a2), (b1, b2):cmp((b2, b1), (a2, a1))
sorted(dict.items(), cmp=re)
distdata = open(distfile, 'w')
for i,j in dict.items():
distdata.write('%s=%s ',i,j)
distdata.close()

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