Write a function called repetitiveLines() that takes two string parameters. The
ID: 3544342 • Letter: W
Question
Write a function called repetitiveLines() that takes two string parameters. The first parameter is the name of an input file that exists before repetitiveLines() is called. The second parameter is the name of an output file that repetitiveLines() creates and writes to. You may assume that the input file is in the current working directory and you should write the output file to that directory as well.
The function repetitiveLines() should write to the output file all of the lines in the input file in which some word, other than an article, occurs more than once. There are exactly three articles in the English language:
Explanation / Answer
I have no idea how to format the code on here, so search and replace the '=-->' with four spaces when you're ready to run this.
ARTICLES = ['a', 'an', 'the']
def repetitiveLines_3a(input_filename, output_filename):
--->input_handle = open(input_filename, 'r')
--->output_handle = open(output_filename, 'w')
--->lines = input_handle.read().split(' ')
--->for line in lines:
--->--->line = line.strip()
--->--->word_counts = {}
--->--->for word in line.strip().split():
--->--->--->if word in ARTICLES:
--->--->--->--->continue
--->--->--->word_counts.setdefault(word, 0)
--->--->--->word_counts[word] += 1
--->--->if [x for x in word_counts.items() if x[1] > 1]:
--->--->--->output_handle.write(line)
--->--->--->output_handle.write(' ')
--->input_handle.close()
--->output_handle.close()
def repetitiveLines_3b(input_filename, output_filename):
--->input_handle = open(input_filename, 'r')
--->output_handle = open(output_filename, 'w')
--->while True:
--->--->line = input_handle.readline().strip()
--->--->if not line:
--->--->--->break
--->--->word_counts = {}
--->--->for word in line.split():
--->--->--->if word in ARTICLES:
--->--->--->--->continue
--->--->--->word_counts.setdefault(word, 0)
--->--->--->word_counts[word] += 1
--->--->if [x for x in word_counts.items() if x[1] > 1]:
--->--->--->output_handle.write(line)
--->--->--->output_handle.write(' ')
--->input_handle.close()
--->output_handle.close()
def repetitiveLines_3c(input_filename, output_filename):
--->input_handle = open(input_filename, 'r')
--->output_handle = open(output_filename, 'w')
--->for line in input_handle.readlines():
--->--->line = line.strip()
--->--->word_counts = {}
--->--->for word in line.split():
--->--->--->if word in ARTICLES:
--->--->--->--->continue
--->--->--->word_counts.setdefault(word, 0)
--->--->--->word_counts[word] += 1
--->--->if [x for x in word_counts.items() if x[1] > 1]:
--->--->--->output_handle.write(line)
--->--->--->output_handle.write(' ')
--->input_handle.close()
--->output_handle.close()
inF = 'catInTheHat.txt'
outF = 'catRepLines.txt'
repetitiveLines_3a(inF, outF)
repetitiveLines_3b(inF, outF)
repetitiveLines_3c(inF, outF)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.