Difference between revisions of "CSC111 Homework 9 2014"
(→Requirements) |
(→Requirements) |
||
(14 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
<br /> | <br /> | ||
<bluebox> | <bluebox> | ||
− | This program deals with text files, writing and reading files, and using Python to process files. | + | This program deals with text files, writing and reading files, and using Python to process files. It is due at midnight on the evening of Thursday, April 10th. |
</bluebox> | </bluebox> | ||
<br /> | <br /> | ||
− | =Problem #1 (50 points)= | + | <tanbox> |
+ | Your programs now should all have functions, a main() function calling various functions, and a last line where the function main() is called. Your programs should sport a header at the top explaining what the program does, an example of its output, along with an example of the format of the input data, if any. Each function should have its own header indicating what it does, what it receives, and what it returns, if any. The solution programs for the homework assignments are good examples of good programming style! | ||
+ | </tanbox> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <tanbox> | ||
+ | There is one optional problem in this set. TAs are '''not allowed''' to help on the optional problem! | ||
+ | </tanbox> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | =Problem #1 (50 points: 20 points for documentation, 30 points for correctness)= | ||
<br /> | <br /> | ||
Write a program called '''hw9a.py''' that reformats text files and rewraps the text in it to a specified width. | Write a program called '''hw9a.py''' that reformats text files and rewraps the text in it to a specified width. | ||
Line 106: | Line 117: | ||
Please enter the file name: <u>CNN.txt</u> | Please enter the file name: <u>CNN.txt</u> | ||
Please enter the wrapping width: <u>30</u> | Please enter the wrapping width: <u>30</u> | ||
− | CNN.txt has been rewrapped | + | CNN.txt has been rewrapped to a width of 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. | * 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. | * 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 | + | * If the text contains 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. |
<br /> | <br /> | ||
<source lang="text"> | <source lang="text"> | ||
Line 174: | Line 185: | ||
cycle. | cycle. | ||
</source> | </source> | ||
+ | <br /> | ||
+ | <tanbox> | ||
+ | '''Final Note''': There is a Python library for wrapping text, and it would work perfectly here. I know that some of you are getting good at finding Python code on the Web (which is a good skill to have), but you '''cannot''' use this library for this homework! | ||
+ | </tanbox> | ||
<br /> | <br /> | ||
− | + | =Problem #2 (50 points: 20 points for documentation, 30 points for correctness)= | |
− | =Problem #2 (50 points)= | ||
<br /> | <br /> | ||
[[Image:USGSEarhquakes.png]] | [[Image:USGSEarhquakes.png]] | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | Write a program called '''hw9b.py''' that will filter earthquake data, extract information about the most severe earthquake | + | Write a program called '''hw9b.py''' that will filter earthquake data, extract information about the most severe earthquake(s) and its/their location(s). |
The data is real data and comes from the the U.S. Geological Survey site ([http://earthquake.usgs.gov/earthquakes/search/ http://earthquake.usgs.gov/earthquakes/search/]), which maintains an up-to-date list of earthquakes that strike the earth and are strong enough to be recorded. | The data is real data and comes from the the U.S. Geological Survey site ([http://earthquake.usgs.gov/earthquakes/search/ http://earthquake.usgs.gov/earthquakes/search/]), which maintains an up-to-date list of earthquakes that strike the earth and are strong enough to be recorded. | ||
Line 208: | Line 222: | ||
Countries where 8.2 quakes were recorded: Chile, Japan | Countries where 8.2 quakes were recorded: Chile, Japan | ||
+ | ;''Hints'': | ||
+ | : 1) you should modify the earthquake files and set a few magnitudes to match 8.2 to verify that your program can handle multiple high-magnitude quakes with identical rating. | ||
+ | : 2) Try this code in the console: | ||
+ | ::<source lang="python"> | ||
+ | L = [1, 1, 2, 10, 20, 3, 20, 20] | ||
+ | L2 = [] | ||
+ | for k in L: | ||
+ | if k not in L2: | ||
+ | L2.append( k ) | ||
+ | L2 | ||
+ | </source> | ||
<br /> | <br /> | ||
Line 214: | Line 239: | ||
Same idea as for Problem 1, but this time your program, called '''hw9c.py''', will take the contents of a text file (whose name is provided by the user), a width (also provided by the user), and it will rewrap the text and store it back in the file formatted as two columns of text. Each column is at most the width specified by the user. | Same idea as for Problem 1, but this time your program, called '''hw9c.py''', will take the contents of a text file (whose name is provided by the user), a width (also provided by the user), and it will rewrap the text and store it back in the file formatted as two columns of text. Each column is at most the width specified by the user. | ||
− | Here are the contents of my CNN.txt file processed by '''hw9c.py''' for a column width of | + | Here are the contents of my CNN.txt file processed by '''hw9c.py''' for a column width of 30: |
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 07:24, 12 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. It is due at midnight on the evening of Thursday, April 10th.
Your programs now should all have functions, a main() function calling various functions, and a last line where the function main() is called. Your programs should sport a header at the top explaining what the program does, an example of its output, along with an example of the format of the input data, if any. Each function should have its own header indicating what it does, what it receives, and what it returns, if any. The solution programs for the homework assignments are good examples of good programming style!
There is one optional problem in this set. TAs are not allowed to help on the optional problem!
Problem #1 (50 points: 20 points for documentation, 30 points for correctness)
Write a program called hw9a.py 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. The file name can be a full path or a relative path. 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 to a width of 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 contains 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.
Final Note: There is a Python library for wrapping text, and it would work perfectly here. I know that some of you are getting good at finding Python code on the Web (which is a good skill to have), but you cannot use this library for this homework!
Problem #2 (50 points: 20 points for documentation, 30 points for correctness)
Write a program called hw9b.py that will filter earthquake data, extract information about the most severe earthquake(s) and its/their location(s).
The data is real data and comes from the the U.S. Geological Survey site (http://earthquake.usgs.gov/earthquakes/search/), which maintains an up-to-date list of earthquakes that strike the earth and are strong enough to be recorded.
Your program will ask the user for the name of a text file containing the data, open it, filter it, and output the location of the strongest earthquake recorded.
Get a sample text file here which I downloaded it from the USGS site. It contains all the earthquakes from Jan 1st, 2014. Once you have downloaded it, unzip it and put it in the same directory where your program is located.
Take a look at it. It is formatted as a CSV file, for Comma-Separated-Value file, meaning that it is a collection of lines of text, where each line contains the same number of fields separated by commas. In general these files have an extension of the form .csv.
The first line of the file explains what the different columns are. The columns of interest for us are the 5th one, the magnitude of the quake, and the last one, the country where the earthquake took place.
Make your program go through all the lines and keep track of the earthquake(s) with the largest magnitude. If several earthquakes have the same largest magnitude, keep track of all of them.
Make your program output the largest magnitude recorded in the file, and the country or countries in which these largest-magnitude earthquakes hit.
Here is an example of the output should generate for the earthquakes.csv file:
Highest magnitude recorded: 8.2 Country where 8.2 quakes were recorded: Chile
If the file had contained three magnitude 8.2 quakes, two in Chile and one in Japan, for example, then the output would have been:
Highest magnitude recorded: 8.2 Countries where 8.2 quakes were recorded: Chile, Japan
- Hints
- 1) you should modify the earthquake files and set a few magnitudes to match 8.2 to verify that your program can handle multiple high-magnitude quakes with identical rating.
- 2) Try this code in the console:
L = [1, 1, 2, 10, 20, 3, 20, 20] L2 = [] for k in L: if k not in L2: L2.append( k ) L2
Problem #3 (Optional and Extra Credit: 20 points)
Same idea as for Problem 1, but this time your program, called hw9c.py, will take the contents of a text file (whose name is provided by the user), a width (also provided by the user), and it will rewrap the text and store it back in the file formatted as two columns of text. Each column is at most the width specified by the user.
Here are the contents of my CNN.txt file processed by hw9c.py for a column width of 30:
Washington (CNN) -- If you're election season. However, the
rich and want to give money to decision left intact the
a lot of political campaigns, current $5,200 limit on how
the Supreme Court ruled much an individual can give to
Wednesday that you can. The any single candidate during a
5-4 ruling eliminated limits two-year election cycle. Until
on how much money people can now, an individual donor could
donate in total in one give up to $123,200 per cycle.
The same original CNN.txt file rewrapped in columns of width 40:
Washington (CNN) -- If you're rich and left intact the current $5,200 limit on
want to give money to a lot of political how much an individual can give to any
campaigns, the Supreme Court ruled single candidate during a two-year
Wednesday that you can. The 5-4 ruling election cycle. Until now, an individual
eliminated limits on how much money donor could give up to $123,200 per
people can donate in total in one cycle.
election season. However, the decision
And now for a width of 20:
Washington (CNN) -- However, the
If you're rich and decision left intact
want to give money the current $5,200
to a lot of limit on how much an
political campaigns, individual can give
the Supreme Court to any single
ruled Wednesday that candidate during a
you can. The 5-4 two-year election
ruling eliminated cycle. Until now, an
limits on how much individual donor
money people can could give up to
donate in total in $123,200 per cycle.
one election season.
Requirements
- The gap between the two columns is 1 space
- If the number of lines in the two columns is different, then make the first column the longer of the two.
- The difference between the number of lines in the two columns is at most one line.
- You may assume that the width specified by the user will always be larger than or equal to the length of the longest word.
- Your program will ask the name of the file first, then the width of the first column, and then it will rewrap the file, overwriting the file with the two columns of text.
- Call your program hw9c.py
Submission
Submit the programs to this URL: http://cs.smith.edu/~thiebaut/111b/submit9.php