1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ira [324]
3 years ago
13

Write a function named repeat_words that takes two string parameters: 1. in_file: the name of an input file that exists before r

epeat_words is called 2. out_file: the name of an output file that repeat_words creates Assume that the input file is in the current working directory and write the output file to that directory. For each line of the input file, the function repeat_words should write to the output file all of the words that appear more than once on that line. Each word should be lower cased and stripped of leading and trailing punctuation. Each repeated word on a line should be written to the corresponding line of the output file only once, regardless of the number of times the word is repeated.

Computers and Technology
1 answer:
madam [21]3 years ago
8 0

Answer:

#section 1

def repeat_words(firstFile, secondFile):

   infile_1=open(firstFile,'r')

   content1=infile_1.read()

   infile_1.close()

#section 2

   import string

   newContent=""

   for char in content1:

       newContent+=char.strip(string.punctuation)

   newContent=newContent.lower()

   newContent=newContent.split()

#section 3

   repeated_list=[]

   for word in newContent:

       if newContent.count(word)>1:

           repeated_list.append(word)

#section 4

   new_repeat_list=[]

   for item in repeated_list:

      while item not in new_repeat_list:

         new_repeat_list.append(item)

#section 5

   outfile=open(secondFile,'w')

   for repeat in new_repeat_list:

       outfile.write(repeat)

       outfile.write('\n')

   outfile.close()

   infile_2=open(secondFile,'r')

   content2=secondFile.read()

   infile_2.close()

   return content2

# section 6

in_file = 'aa.txt'

out_file = 'cpu.txt'

print(repeat_words(in_file, out_file))

Explanation:

#section 1

it defines a function that takes two arguments. It opens the first argument in the read mode and stores all it's content in a string variable "content1". finally the file is closed.

#section 2

In this section the string module is imported which allows for easy manipulation of string variables.

A new string variable is created that would store the already stripped file.

<em>strip(string.punctuation)  </em>strips every character of any leading or trialing punctuation.

<em>lower()  </em>converts string to lover case.

<em>split() </em>converts a string to an array of sub-string, i.e an array of words.

#section 3

This section collects all the words in the array and places them in a list.

#section 4

Utilizes a For and a While loop to ensure that no word repeats and stores it in a new list.

#section 5

Opens a new file and writes all the content into that new file and then closes the file.

re-opens the file, reads and returns the content of the file.

# section 6

This is added in order to test the function and print the result

I used your question to test the function and attached the result.

cheers!

You might be interested in
Since cam systems regulate and self-mange the manufacturing process, you classify them as
Anna007 [38]

Answer:

The CAM means Computer-Aided Manufacturing, and it monitors as well as manages on the self basis the complete manufacturing process, and hence it is being categorized as the Programmable Automation, that is responsible for the production of the products in various batches. It is in a real sense an Automated Manufacturing.

Explanation:

Please check the answer section.

3 0
3 years ago
Declare an array named scores of twenty-five elements of type int .
Katen [24]
Declaration for the Visual Basic for Applixations

Dim scores(25) as Integer
4 0
3 years ago
A _____ provides a file-system interface which allows clients to create and modify files.
stiv31 [10]
The correct answer is a file-server system. Hope this helps!
3 0
4 years ago
You have been on the phone with a user in a remote office for 30 minutes troubleshooting their minor desktop problem. No matter
Alecsey [184]

Answer:

D

Explanation:

First when you are troubleshooting a client your main goal is to solve their issue, you dont want to say hey call later i cant help you or say call someone else because picture this you need help and someone hangs up on you or says (B.) or (C.) it comes off as rude i would say. Regarding (A.) im not 100% sure what exactly do you mean by user's site? But asking for their manager (D.) or someone else (preferably higher up) seems to be the right action to be taken.

4 0
3 years ago
Algorithm to print the first 10 odd numbers​
77julia77 [94]

Answer:

I have the code written in c++ but the logic will be the same you just have to change the syntax ( if you're writing different language)

Explanation:

please mark brainliest

4 0
2 years ago
Other questions:
  • When computers connect to one another to share information, but are not dependent on each other to work, they are connected thro
    7·1 answer
  • _____ supplement operating system software in ways that increase the security or capabilities of the computer system. firewalls,
    15·1 answer
  • What cannot be performed using windows task manager
    10·1 answer
  • Your teacher needs to keep track of a biology experiment's results for all students, to
    7·2 answers
  • Why are modern manufacturing techniques preferred over those historically used by artisans?
    13·1 answer
  • Although you are not a full administrator, you are asked to manage a protected document, allowing customized access to any inter
    7·1 answer
  • What determines the color of a rock?
    11·2 answers
  • A bot can use a _______to capture keystrokes on the infected machine to retrieve sensitive information.
    10·1 answer
  • Hey i need some help with code.org practice. I was doing a code for finding the mean median and range. the code is supposed to b
    14·1 answer
  • Write getfirstroot and getsecondroot to return the first and second roots. return a domain_error is there is no first or second
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!