Difference between revisions of "CSC111 Homework 9 2014"

From dftwiki3
Jump to: navigation, search
(Problem #1)
(Problem #1)
Line 16: Line 16:
  
 
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:
 
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:
 
+
<br />
 
<source lang="text">
 
<source lang="text">
 
Washington (CNN) -- If you're
 
Washington (CNN) -- If you're
Line 22: Line 22:
 
a lot of political campaigns,
 
a lot of political campaigns,
 
the Supreme Court ruled
 
the Supreme Court ruled
Wednesday that you can. The 5-4
+
Wednesday that you can. The
ruling eliminated limits on how
+
5-4 ruling eliminated limits
much money people can donate in
+
on how much money people can
total in one election season.
+
donate in total in one
However, the decision left
+
election season. However, the
intact the current $5,200 limit
+
decision left intact the
on how much an individual can
+
current $5,200 limit on how
give to any single candidate
+
much an individual can give to
during a two-year election
+
any single candidate during a
cycle. Until now, an individual
+
two-year election cycle. Until
donor could give up to $123,200
+
now, an individual donor could
 +
give up to $123,200 per cycle.
 +
</source>
 +
<br />
 +
If the program had been set to wrap at 10, this is what it would have stored in the file:
 +
<br />
 +
<source lang="text">
 +
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.
 
per cycle.
 
</source>
 
</source>

Revision as of 17:34, 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 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.