Question
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 an input file that exists before LineStats is called 2. outFile, a string, the name of an output file that Linestats creates and writes to Important: The input file contains only upper and lower case letters and white space (no punctuation marks or other special characters). For example, if the following is the content of the file mary. txt: Mary had a Little Lamb Its fleece was white as snow And everywhere that Mary went The Lamb was sure to go The following function call: >>> inFile mary.txt >>> outFile ,marySt at s . txt >>> lineStats(inFile, outFile) should create the file maryStats.txt with the content: 12 6 14 9 17 8 11 7
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Following is the Python3 program which counts the number of consonants and vowels from one text files and writes the stats to another text file. Screenshots of the output and the code are included at the end. Comments are placed for better understandability of the program.

Python3 CODE:

# Function to count vowels and consonants line by line

def vowel_count(line, outFile):

    # Opening the file to write stats to

    f2 = open(outFile, "a")

    vowels = set("AEIOUaeiou")

    cons = set("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ")

    # Counters for vowels and consonants

    countV = 0

    countC = 0

    # Loop to count vowels and consonants in each line

    for c in line:

     if c in vowels:

     countV += 1

     elif c in cons:

     countC += 1

    print("Vowels : ", countV)

    print("Consonants : ", countC)

    L =[str(countV),"\t", str(countC)+"\n"]

    # Writing the stats to the file

    f2.writelines(L)

    f2.close()

        

# Function to read lines from the inFile

def lineStats(inFile, outFile):

    f = open(inFile)

    line = f.readline()

    while line:

        print(line)

        # Call to the function to count vowels per line

        vowel_count(line, outFile)

        print("\n")

        line = f.readline()

    # Closing the file

    f.close()

# Driver code

# Opening file to read lines from

inFile = 'mary.txt'

outFile = 'maryStats.txt'

# Call to the function lineStats and pass the names of the files

lineStats(inFile, outFile)

SCREENSHOTS:

vowels file.py - Temp-Visual Studio Code Eile Edit Selection View Go Debug Terminal Help EXPLORER vowels..file.py × OPEN EDITORS x e vowels_file.py TEMP Function to count vowels and consonants line by tine def vowel count (line, outFile) # Opening the file to write stats to f2-open(outFile, a) vowelsset( AEIOUaeiou cons set (bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPORSTVWXYZ ) # Counters for vowels and consonants countv countc # Loop to count vowels and consonants in each line for cin line D a.out Account.java Appointment.java AppointmentTes Dprogram.bin calculator.py CheckingAccoun CirclePanel.class circlePaneljava CirclePanelŞMov ¡f c in vowels: countv += 1 elif c in cons: countc1 D countries.dat D data.bin 目data1.txt print(Vowels , countV) print(Consonantscountc) L -Istr(countv),\t, str(countc)+n # Writing the stats to the file f2.writelines (L) cpu usage.sh data.txt 22 # Function to read lines from the inFile E data2.txt def linestats (inFile, outFile): f open(inFile) line - f.readline() while line: Demo Unsorted Doctor.class P Doctorjava doctor.java print (line) # Call to vowel_count(line, outFile) print(In) line = f, readline() the function to count vowels per Line C driver.c expense.txt C file_handling_1.c # Closing the file f.close() OUTLINE Python 2.7.15 64-bit * Ο ㅿ O O 2 면 Live Share B python l B vowels-file.py n 1, Col 1 Tab Size:4 UTF-8 LF Python

OUTPUT:

Do leave a thumbs up if this helps. Feel free to ask doubts regarding this answer in the comments section below.

Add a comment
Know the answer?
Add Answer to:
Using python Question 13 (20 points) Write a function named Linestats that finds the number of...
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...

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

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

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

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

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

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

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

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