Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Read all of the instructions Instructions Write a Python program that reads each line in the file input.txt, reverses the words in each line, and

Read all of the instructions

Instructions

Write a Python program that reads each line in the file input.txt, reverses the words in each line, and writes them to output.txt.

For example, if the file input.txt contains the lines

 

Mary had a little lamb

its fleece was white as snow

and everywhere that Mary went

the lamb was sure to go

then output.txt contains the lines

 

lamb little a had Mary

snow as white was fleece its

went Mary that everywhere and

go to sure was lamb the

Design restrictions

The contents of input.txt must be read without a loop, in a single statement.

The program should minimize the number of code lines between the opening and closing of a file.

This is the code I have but im not quite there

image text in transcribed

with open("input.txt", "r") as input_file, open("output.txt", "w") as output_file: input_string = input_file.read() input_string = input_string.split (" ") [::1] \#input_string = input_string [::1] output_file.writelines(" ".join(input_string[::-1]))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions

Question

=+ What is the nature of the contracts or agreements with unions?

Answered: 1 week ago

Question

=+What is the procedure for labor relations in the workplace?

Answered: 1 week ago

Question

=+ Are ballots compulsory?

Answered: 1 week ago