Difference between revisions of "CSC111 Homework 9 2014"
(→Requirements) |
(→Requirements) |
||
Line 99: | Line 99: | ||
<br /> | <br /> | ||
* Your program should ask for the name of the file to be rewrapped. | * Your program should ask for the name of the file to be rewrapped. | ||
− | * Your program should ask next for the width at which to wrap the text inside the file. | + | * Your program should ask next for the width at which to wrap the text inside the file. Here is an example where the user input is underlined. |
Please enter the file name: <u>CNN.txt</u> | Please enter the file name: <u>CNN.txt</u> |
Revision as of 17:52, 2 April 2014
--D. Thiebaut (talk) 18:25, 2 April 2014 (EDT)
Contents
This program deals with text files, writing and reading files, and using Python to process files.
Problem #1
Write a program that reformats text files and rewraps the text in it to a specified width.
Here is an example. Assume you have a text file called CNN.txt that contains this text (taken from Google news on 4/2/14):
- Washington (CNN) -- If you're rich and want to give money to a lot of political campaigns, the Supreme Court ruled Wednesday that you can. The 5-4 ruling eliminated limits on how much money people can donate in total in one election season. However, the decision left intact the current $5,200 limit on how much an individual can give to any single candidate during a two-year election cycle. Until now, an individual donor could give up to $123,200 per cycle.
Let's assume that the program is set to rewrap the text in the file to a width of 30 characters, here is what it would write back in the file, after having read it:
Washington (CNN) -- If you're
rich and want to give money to
a lot of political campaigns,
the Supreme Court ruled
Wednesday that you can. The
5-4 ruling eliminated limits
on how much money people can
donate in total in one
election season. However, the
decision left intact the
current $5,200 limit on how
much an individual can give to
any single candidate during a
two-year election cycle. Until
now, an individual donor could
give up to $123,200 per cycle.
If the program had been set to wrap at 10, this is what it would have stored in the file:
Washington
(CNN) --
If you're
rich and
want to
give money
to a lot
of
political
campaigns,
the
Supreme
Court
ruled
Wednesday
that you
can. The
5-4 ruling
eliminated
limits on
how much
money
people can
donate in
total in
one
election
season.
However,
the
decision
left
intact the
current
$5,200
limit on
how much
an
individual
can give
to any
single
candidate
during a
two-year
election
cycle.
Until now,
an
individual
donor
could give
up to
$123,200
per cycle.
Requirements
- Your program should ask for the name of the file to be rewrapped.
- Your program should ask next for the width at which to wrap the text inside the file. Here is an example where the user input is underlined.
Please enter the file name: CNN.txt Please enter the wrapping width: 30 CNN.txt has been rewrapped at the 30 characters.
- Your program should then open the text file, read its contents, rewrap it to the specified length, and write it back to the file.
- The length of a line in the rewrapped text should be less than or equal to the specified width (this is important and you will lose points if your program wraps lines at a width strictly less than the specified length.
- If the text contain words that are longer than the specified wrapping width, they should not be skipped, and included in the wrapped text. For example, here's the contents of the file if the wrapping width is specified to be 9, which is less than the length of some words.
Washington
(CNN) --
If you're
rich and
want to
give
money to
a lot of
political
campaigns,
the
Supreme
Court
ruled
Wednesday
that you
can. The
5-4
ruling
eliminated
limits on
how much
money
people
can
donate in
total in
one
election
season.
However,
the
decision
left
intact
the
current
$5,200
limit on
how much
an
individual
can give
to any
single
candidate
during a
two-year
election
cycle.
Until
now, an
individual
donor
could
give up
to
$123,200
per
cycle.