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
Consider the following protocol for concurrency control. The database system assigns each transaction a unique and strictly incr
nasty-shy [4]

Answer:

(a) yes, this protocol allows only serializable schedules for transactions as due to this the system maintains it's consistency. As in this protocol a unique transaction id is being assigned and with the help of that transaction id the system would be able to identify the process which has taken place in what particular order. For example, in case of bank transfers

balance = 1000 transaction id 100

write ADD 200 transaction id 101

write SUB 1100 transaction id 102

write ADD 900 transaction id 103

in here with the help of transaction id we can check which operation has happened in which order, if not then some operation will not happen like 102 immediately after 100 and skipping 101

(b) the modified version of this protocol would be to also consider the time of transaction and take this factor in the consideration

4 0
4 years ago
Are used in the Excel application to create calculations
OlgaM077 [116]

Answer:

I think the answer is formulas

6 0
3 years ago
Read 2 more answers
At your job, you often have to address letters to the customer support manager, Tyson Kajewski. The problem is that you are cons
krek1111 [17]
Input his name in the dictionary function, you can also copy the given name an paste as much as you need
5 0
4 years ago
Look at the following assignment statements:
ziro4ka [17]

Answer: number 2 is the correct way to do it

Explanation:

3 0
3 years ago
Sites like Zillow get input about house prices from a database and provide nice summaries for readers. Write a program with two
natka813 [3]

Answer:

tyhgfrd

Explanation:

8 0
3 years ago
Other questions:
  • Which devices are managed through device management?
    15·2 answers
  • A girl scout troop with 10 girl scouts and 2 leaders goes on a hike. When the path narrows, they must walk in single file with a
    12·1 answer
  • Charli's password was changed a couple of weeks ago because someone figured it out. Can someone go to a password generator and f
    5·2 answers
  • 2.1. The stream cipher described in Definition 2.1.1 can easily be generalized to work in alphabets other than the binary one. F
    10·1 answer
  • Describe at least two other companies that are direct or indirect competitors to your company. Explain how you will differentiat
    15·1 answer
  • How many of the colonists of Jamestown died before they made it to shore, due to the difficult voyage?
    15·1 answer
  • Scenario
    7·1 answer
  • Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither.
    5·1 answer
  • List any three positive impact of a computer​
    9·1 answer
  • In additon to setting up services, what other tasks does a sysadmin have to keep in mind? check all that apply.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!