Question: import sys def read _ book ( filename ) : contents = [ ] with open ( filename ) as book: for line in book.readlines
import sys
def readbookfilename:
contents
with openfilename as book:
for line in book.readlines:
line line.strip
# Strip off the trailing newline character
line line.split # Split the line into separate fields
line intline
contents.appendline
contents.sortkeylambda x: x
return contents
def findlongestlinecontents:
longline
for i line in enumeratecontents:
if lenline lenlongline:
longline linei
return longline
def calculateaveragelengthcontents:
totallength sumlenline for line in contents
averagelength roundtotallength lencontents
return averagelength
def writeresultsbookname, contents:
filename ffilenamebook.txt
with openfilenamew as summaryfile:
summaryfile.writeTTL
longestline findlongestlinecontents
summaryfile.writef Longest line longest: longestlongest
averagelength calculateaveragelengthcontents
summaryfile.writefAverage length: averagelength
for line in contents:
summaryfile.writeflinen
def main:
filename sysargv:
contents readbooksysargv
writeresultsfilename contents
if namemain:
main
Structure
File
library.py
Input
a text file such as TTCtxt
one input file for each book
Output
text file such asTTCbook.txt
one output file for each book
Problem
Youre an intern at the Library of Congress. You are given a collection of text files where each file corresponds to a book. Each file is named with a threeletter code for the book it contains. Examples are like TTCtxt WOO.txt TTLtxt and other names too. There could be many such input files and we may not know how many.
Each line in the file looks like this: one line from the book and the line number, separated by a For example: It was the best of times, it was the worst of times,"
You discover though, that the content of each file is scrambled so that the lines are not in order.
Your task is to write a program that reads each of these book files and does a few things:
Find the longest line from the book along with its line number.
Calculate the average length of the lines in the book.
Unscramble the lines of text in order of line number.
Once your program has done that, it needs to save this information in a new file called book.txt The format would be:
first line is the threeletter code
second line is the longest line with line number and content as shown
third line is the average length
lines to end are the contents of the book in line order
Example Run
As an example, for this input
python library.py TTLtxt
Your code will generate a file named as TTLbook.txt and the first few lines of that output file would look like this:
TTL
Longest line : EcclesiastesWho can fathom the soundless depths?two men out of all
Average length:
Twenty Thousand Leagues Under the Seas
An Underwater Tour of the World
JULES VERNE
If there are multiple lines with the same length as the longest, use the line number to decide: the line that appears later is considered as "longest". Round the average to the closest integer.
And thats it Youve gathered useful information, unscrambled the text, and written the information to a new file.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
