# Chapter 18 Exercises¶

Here are just the first 9 lines of the data for city pollution. The data is in the order: City, PM 10, PM 2.5. This data set will be used for exercise questions 1 - 12.

Aberdeen, SD :13 :8
Akron, OH :18 :11
Albany, GA :18 :11
Albany-Lebanon, OR :14 :8
Albuquerque, NM :12 :7
Alexandria, LA :20 :12


If you want to see all of the data click on the Show button below. Once it appears, you can hide it again by clicking on the Hide button.

This data set shows the monthly Dow Jones averages from 1989 to 2001. The data is in the order: Date, Open, High, Low, Close, Volume. This data will be used for exercise questions 13 - 20.

3-Dec-01,9848.93,10220.78,9651.87,10021.57
1-Nov-01,9087.45,10054.58,8987.61,9851.56
1-Oct-01,8845.97,9626.54,8659.9,9075.14
4-Sep-01,9946.98,10238.5,7926.93,8847.56
1-Aug-01,10527.38,10663.07,9829.35,9949.75
2-Jul-01,10504.95,10758.14,10049.38,10522.81
1-Jun-01,10913.57,11236.68,10313.4,10502.4
1-May-01,10734.05,11436.42,10638.48,10911.94
2-Apr-01,9877.16,10973.15,9303.48,10734.97


If you want to see all of the data click on the Show button below. Once it appears, you can hide it again by clicking on the Hide button.

1. Fix 5 errors in the code below so that the code runs correctly and prints the pollution for all cities that start with the letter A.

Add a " before the ) in line 1. Change line 2 to readline instead of readLine. Add a : at the end of line 3. Add a , before city on line 7. Change infile to inFile on line 9.

2. Fix the errors in the code below so that it prints the average PM values of only the cities that start with “A”.

In line 10, city should equal values[0]. In line 11, it should be 0 not -1. Indent the count so it is in the body of the if statement.

3. Fix the 5 errors in the code below so that it runs and prints the largest PM 2.5 value and the city that has that value.

Add a ) at the end of line 1. Change line 3 to close. Set max25 to 0 in line 6. Add in on line 7 before lines. Add : at the end of line 10.

4. The code below prints all the lines that have a city that starts with an “A”. Change it so that it prints out all lines that have a city that starts with “A” or “B”.

Add an or clause or an elif.

5. Fix the indention below for the code to correctly find and print the lowest 2.5 value and city.

Indent lines 8 through 12 as shown below.

6. Fix the code so that it prints out the min value and the cities only when the min value is even.

On line 2, put the “r” in quotations. On line 3, change readline to readlines(). On line 10, split on the “:” not a space. On the last line, indent the print statement to be in the body of the if clause.

7. Fix the indention on the lines below so that it correctly prints the average PM 2.5 value.

Fix the indention on lines 8-11 as shown below.

8. Write a procedure that takes in the file name as a parameter and prints out all the cities that start with a vowel (a,e,i,o, or u) and their associated pollution values.

Define a procedure like shown below

9. Turn the following code into a procedure. Pass the input file and the amount of PM 10 to the procedure. It will print the city name and the pollution values for all cities that have that much PM 10 pollution or more.

Define the procedure as shown below. Be sure to call the procedure to test it.

10. Write a function that returns the average PM 2.5 of cities that start with “L”.

Write the function as shown below.

11. Change the following code into a procedure that prints the city name and pollution values for all cities that have a PM 2.5 of less than some passed value. Pass in the input file and the amount of pollution.

Define a procedure that takes the input file and the amount. Print all the cities that have lower than the passed amount for PM 2.5.

12. Write a procedure that takes the name of a city and prints the pollution values for that city if it is found.

Define the procedure and call it. Be sure to pass the city name to the procedure.

13. Complete the code at the # so that it prints out the date with the biggest loss from open to close.

Complete as shown below.

14. Fix the errors below so that the procedure prints all the dates where the Dow Jones gained more than 300 points from open to close.

Fix the indentation of the body of the for loop. Line 3 should be line in lines instead of lines in lines. Line 4 should be split(","). Line 5 should be float(line[1]). Line 7 should be >.

15. The code currently prints the date that the Dow Jones had the biggest percent loss. Change 2 lines so that it prints the date with the biggest percent gain.

Change line 3 to 0 and line 9 to >.

16. The code below prints all the dates and high price for dates that occur on the first day of the month (i.e. January 1, February 1…). Change it so that it prints the date and low price for all the dates that occur in June.

Split the name by the space and find the first letter of the second item in the new list.

17. Write a procedure that takes in the stocks file and a string of the last two digits from the years 1989 - 2001 (i.e. “89”) and prints the difference between the highest and lowest points of that year.

Write a procedure like shown below.

18. Write a function that takes the stocks file and the abbreviation for a month (i.e. Jan, Feb) as parameters and returns the average value of all the closing prices during that month from all the years given (Hint: Use a counter variable).

Define a function like below.