Question

Python 3.6

Question 12 (2θ points) write a function named uniqueWords that counts how many different words there are in each line of an input file and writes that count to a corresponding line of an output file. The input file already exists when uniqueWords is called. uniquewords creates the output file. Input. The function uniquewords takes two parameters: ◆ inFile, a string that is the name of text file that is to be read. The file that inFile refers to contains only lower case letters and white space (no punctuation marks or other special characters) · outFile, a string that is the name of the file to which uniqueWords writes its out put. The input file is in the current working directory. uniquewords should create the output file in that directory. output. For each line of infile, uniquewords should write a corresponding 1line to outFile containing a single integer: the number of unique words on the line If the content of the file turn.txt is below, a time to build up a time to break dowrn a time to dance a time to mourn a time to cast away stones a time to gather stones together the function call uniquewords (turn.txt, turnout.txt) should create a file named turnout.txt with this content 7 5 8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def Unique(in_f, out_f):
fp = open(in_f,"r")
  
data = fp.read() #reading the content of the file
data = data.splitlines() #splitting into seperate lines
  
count = [] #contains the count of unique elements
for line in data:  
s = line.split(" ") #splitting line into words
unique = [] #list will contain unique words
for word in s:
if word not in unique:
unique.append(word)
  
count.append(str(len(unique))+"\n")
  
  
fp.close()
  
outp = open(out_f, "w") #the output file
  
outp.writelines(count) #printing the result to the output file
  
Unique('infile.txt', 'out_f.txt')

Pythin -PyDev - database/march CountUnique py - Eclipse File Edit Scurce Refactoring Navigate Search Prjet Pyde Run Wirio HelPythin -PyDev- database/march Intile.tat- Eclipse File Edit Navigale Search Prejent Pyciew Run Wiricw Help Quick Access| | |Pythin -PyDev- database/march out t.tat- Eclipse File Edit Navigale Search Prejent Pyciew Run Wiricw Help |Quick Access! :| 凹If there is anything that you do not understand, or need more help with, then please mention it in the comments section.

Add a comment
Know the answer?
Add Answer to:
Python 3.6 Question 12 (2θ points) write a function named uniqueWords that counts how many different...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using python Question 13 (20 points) Write a function named Linestats that finds the number of...

    Using python Question 13 (20 points) Write a function named Linestats that finds the number of consonant and vowel letters on each line of a file and writes those statistics to a corresponding line of a new file, separated by a space. Definitions: A vowel letter is one of a, e, i, o, u, along with the corresponding uppercase letters..A consonant letter is not a vowel letter. The function linestats takes two parameters: 1. inFile, a string, the name of...

  • Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two...

    Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file exists when wordLengths is called; wordLengths must create the file outFile. The input file contains only letters and white space For each line of the input file, wordLengths should identify the length of the longest word...

  • Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the...

    Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file inFile exists when mostFrequent is called; mostFrequent must create outFile. The input file contains only lower case letters and white space. The function mostFrequent identifies the letter(s) that appear most frequently on each line of inFile and writes them to a corresponding line of...

  • help Question 12 (20 points) Write a function named inverse that takes a single parameter, a...

    help Question 12 (20 points) Write a function named inverse that takes a single parameter, a dictionary. In this key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. dictionary each The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course For...

  • Problem 3 Write a function named repeat_words that takes two string parameters: 1. in_file: the name...

    Problem 3 Write a function named repeat_words that takes two string parameters: 1. in_file: the name of an input file that exists before repeat_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...

  • in pyhton For example, the following would be correct input and output >» student_courses ('Elise:[1. Nelson :['cS1e0, MATH111']. 'Justin:t CS100']) >>> print (inve...

    in pyhton For example, the following would be correct input and output >» student_courses ('Elise:[1. Nelson :['cS1e0, MATH111']. 'Justin:t CS100']) >>> print (inverse(student_courses)) ('cs100': ['Nelson', Justin'], 'MATH111: [Nelson']) Question 13 (20 points) Write a function named filestats. The function filestats takes two parameters 1. 2. inFile, a string that is the name of an input file outFile, a string that is the name of an output file The function ftlestats should read and analyze each line of the input file...

  • Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • python: Write a function named "write_values" that takes a key-value store mapping strings to strings as...

    python: Write a function named "write_values" that takes a key-value store mapping strings to strings as a parameter and writes the values of the input to a file named "persuade.txt" with one element per line. If a file named "persuade.txt" already exists it must be overwritten. The function should not return any value

  • ****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of...

    ****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of a file. The program encoded sentences in the file and writes the encoded sentences to the output file. Enter the file name: messages.txt Output: encoded words are written to file: messages.txt.swt The program reads the content of the file and encodes a sentence by switching every alphabetical letter (lower case or upper case) with alphabetical position i, with the letter with alphabetical position 25...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT