A function called freq distribution with two parameters: infile and distfile. Th
ID: 3562706 • 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 characters. That is, the characters should be listed in alphabetically sorted order in the output file. Each line of the file should contain the character 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.
Explanation / Answer
def freq_distribution(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()
distdata = open(distfile, 'w')
for i,j in sorted(dict.items()):
distdata.write('%s=%s ',i,j)
distdata.close()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.