Question

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 on the line and how many words of that length there are. It should write the length of the longest word, followed by a space, followed by how many words of that length there are on the line to a corresponding line of outFile. If a line of the input file is empty the corresponding line of the output file should also be empty. For example, if the file zax.txt contains the following lines: There they stood Foot to foot Face to face then the function call wordLengths(zax.txt, zaxOut.txt) should produce an output file with the following lines: 5 2

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

def wordLengths(inFile,outFile):
filepath = inFile
f= open(outFile,"w+")

with open(filepath) as fp:
    line = fp.readline()
    cnt = 1
    print(line)

    while line:
      line = fp.readline()
      char=0
      word=0
      max=0
      for i in line:
        char=char+1
        if(i==' '):
          word=word+1
          if(max < char):
            max=char
           
          char=0
      cnt += 1
      f.write("%d %d\r\n" % ((max-1),word))
   
f.close()


wordLengths("zaxx.txt","output.txt")

Files D main.py D output.tx Osaved Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux share run main.py 2 filepa

Add a comment
Know the answer?
Add Answer to:
Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two...
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
  • Python 3.6 Question 12 (2θ points) write a function named uniqueWords that counts how many different...

    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...

  • 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...

  • 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...

  • 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...

  • C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the r...

    C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything.     Your function should be named...

  • hey please answer in python 3 This is the test for these questions. it has to...

    hey please answer in python 3 This is the test for these questions. it has to be exactly the same as the Expected output. notice my code is very close to getting it but im always off by 0.01 or 0.02. Thank you for the help! In this exercise, you will build a program to process files containing paragraphs. The aim of the program is to simply count the number of words and compute their length. The file, however, may...

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