USING PYTHON 12-Write a function named mostFrequent that takes two parameters.1-
ID: 3833631 • Letter: U
Question
USING PYTHON
12-Write a function named mostFrequent that takes two parameters.1-inFile, a string that is the name of the input file and 2-outFile a string that is the name of output file.The input file exists when mostFrequent is called, mostFrequent must create outFile. The input File contains only lower cases letter and white space.The function mostFrequent identifies the letter(s) that appear most frequently on each line of the inFile and write them to corresponding lines of the outFile. If more than one letteron the line has the same frequency, they should each be written to outFile.Each letter should be written to the line only once.
Explanation / Answer
def mostFrequent(nfile, outfile):
with open(outfile, "w") as fw:
with open(infile, "r") as fh:
for line in fh:
count_list [0]*26
for c in line:
if c != ' ':
i = ord(c) - ord('a')
count_list[i] += 1
max_count = max(count_list)
for i in range(0, 26):
if count_list[i] == max_count:
fw.write(chr(ord('a') + i))
fw.write(" ")
# code link: https://paste.ee/p/pSF6F
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.