Exercise 1 - File I/O and Dictionaries

Thanks to Dr. Alan McLeod for the content of this exercise.

Here is some historical weather data for the city of Toronto: TorontoWeatherData.csv from 1938 to August, 2010, downloaded from Environment Canada's National Climate Data and Information Archive. It consists of 7 columns of numeric data:

The columns are separated by commas (.commaseparatedvalues). Although your operating system may recommend opening the file in a spreadsheet, all you need is a text editor.

Write a function readData(...) that reads in the information from this file and produces a list of dictionaries. Each dictionary contains one year's weather data with values for the year, month, meanT, maxT, minT, rain and snow. The function must have a single string parameter containing the full path to the file and must return the list of dictionaries.

Be sure to save the code for this function; you will be using it in next week's lab. Don't forget to handle exceptions.

Exercise 2 - The os Module

The os, os.path and sys modules contain many useful utilities for exploring the file system. To see a list of what these modules provide, select Python Manuals from the Python program group or Help -> Python Docs from within IDLE. Click on the Index tab and enter the name of the module in the text box. Double-click on the <name> (module) option shown in the list.

Write a function shallowSearchForPythonFiles(...) with a single string parameter containing a directory's path. The function must return a list containing the full paths for every Python file in the directory (files ending in ".py"). Don't forget to catch any exceptions that may occur. Hint: the listdir(...) function may be of use.

Write a companion function deepSearchForPythonFiles(...) which also has a single string parameter containing a directory's path. The function must return a list containing the full paths for every Python file in the directory and all subdirectories. Hint: the walk(...) function would be very helpful here.

Here is a sample solution for the above.

Exercise 3 - Timing Code Execution

Import the time module and call the clock() function both before and after the call to shallowSearchForPythonFiles(...) in Exercise 2. Subtract the value returned by the first call from the value returned by the second call. How long did the function take to execute?

Now do the same thing for deepSearchForPythonFiles(...). This function will likely take longer; why?

Exercise 4 - Finding the Max

Write a function removeMaximum(...) with a single list parameter containing 5 numbers or more. If the list does not contain that many, return immediately. Otherwise, find the maximum value in the list and remove it using del.

Compose a main() function that creates a suitable list of numbers (hint: try using randint(...) and a loop). Print the list, call removeMaximum(...) using the list several times, and then print the list again. Why does the deletion carry over from removeMaximum(...) into main()?

Here is a sample solution for the above.

More Exercises

You should now be able to do any of the Programming Exercises at the end of Chapter 7 of the textbook; exercises 3, 6 and 7 look interesting.