CS 111
Fall 2016
Introduction to Computer Science
HW07: Weather statistics
Due: Monday, 10/10 at 23:55
Introduction
When you write a function, you are giving a name to a collection of operations. We do this in non-programming life all the time. "Make breakfast" is a name for a complex sequence of operations, most of them conditional ("if the milk hasn't gone bad, get the bowl out for cereal, otherwise if there is bread, get out the toaster"). In a computer program, writing a function enables you to name a complex operation and reuse it anywhere else in your program—even in another function. By naming operations in this way, we can decompose very complex problems into simple parts, which enables us to think more effectively about the problems.
In this assignment, you will write some functions and then use those functions to solve a problem involving weather data.
The program
Your program, called weatherstats.py
, will read data from
this file of daily high temperatures
recorded at the Minneapolis-St. Paul airport between 1939 and 2014, and produce
a report summarizing some of the interesting features of the temperature
data. Your program should accept the name of the data file as a
command-line argument, and then produce output looking something
like this:
Note: My numbers in the example above are fictional. I made them up to demonstrate the kinds of display I want to see. Your results will be different from these.
Though you are free to implement much of your program however you wish, I want you to start by writing the following three functions:
You may download a skeleton file as a starting point.
Note that the Python instruction pass
is just a place-holder
for the function body that does nothing at all. You will replace the
pass
in each case with the actual code required by these three
functions.
The idea here is that if you write these three fairly simple functions, your main program will be able to exploit these functions (in some cases repeatedly) to perform the overall task of the program. Of course, you are free to define additional functions if you find it useful to do so. But you should definitely implement the three functions described above, and use them in your main program where appropriate.
The raw data for this exercise came from the United States Historical Climatology Network.
Some suggestions
Before you start, develop an incremental development plan like the one I described in class a week or two ago. For example, Stage 1 might be to implement the
mean
function and test it like so:print(mean([23])) print(mean([4, 4, 4, 4])) print(mean([5, -2, 3, 4])) print(mean([]))Stage 2 could be to implement
standardDeviation
, and test it using example data from the Wikipedia page, etc.- Don't know the formula for standard deviation? See the Wikipedia article, notably the examples in the "Basic examples" section and the definition in the "Discrete random variable" section.
- Don't forget that functions can call functions. For example,
standardDeviation
will probably want to callmean
. - If you have a list where the first element is special (e.g.,
myList = [1986, -4, 9, 6,...]
), you can get a list containing everything other than the first element using slicing, like so:myList[1:]
. - Command-line arguments are accessible via
sys.argv
. See the skeleton file for more details. - You already know how to iterate through lines of a file via a
for
loop. This is sufficient to do this assignment. However, you may wish to peruse Zelle §5.9 titled "File Processing."
This is a somewhat long assignment. Start early! Ask questions. Have fun!