# Lab 6, CS0008, Spring 2015 # # Files and 2-dimensional arrays # # Specification of the input file: # # The file has 3 header lines. # After that, each line contains the average monthly temperatures # for a year (separated by spaces) starting with January. # # Use nested lists to represent the temperatures. # Each element of the list is a list of 12 measurements, one for each month. # # Let's use a smaller example: suppose we have only 4 months, and the data # is the following: # #24.7 25.7 30.6 47.5 #16.1 19.1 24.2 45.4 #10.4 21.6 37.4 44.7 # # # Your list will be: # [[24.7,25.7,30.6,47.5],[16.1,19.1,24.2,45.4],[10.4,21.6,37.4,44.7]] # # We'll number the years 0 to 2, and the months 0 to 3. # # temperature[2][3] is 44.7, the temperature for year 2, month 3. # # To make sure you understand lists of lists, here is an example # of a list of lists using the shell: #>>> l = [[12.0,1.0,3.0],[2.0,3.0],[66.1,77.1]] #>>> l[0] #[12.0, 1.0, 3.0] #>>> l[1] #[2.0, 3.0] #>>> l[2] #[66.1, 77.1] #>>> l[0][0] #12.0 #>>> l[0][2] #3.0 #>>> l[2][0] #66.1 #>>> l[2][1] #77.1 #>>> # # # Note: a couple of the functions are almost the same. None of them # are very large. When possible one function should call another. def inputTemperatureData(filename): '''Open the specified filename, read past the three-line header, read in the temperature data on the file, and close the file. Return the temperature data in the form of a list of lists, where each list element is a list of 12 temperature readings.''' # Hint: use readline to read the first three lines, and then # a while loop to read the numbers. # # suppose variable line contains a line of temperature readings. # listOfNums = line.split() will create a list of strings (and # discard the '\n' character. # Here is an example in the python shell: #>>> line = "34.55 43.55 23.7 4.0\n" #>>> listOfNums = line.split() #>>> listOfNums # ['34.55', '43.55', '23.7', '4.0'] # # Hint: once you have the list of strings, go through the list # with a for-loop, to create a list of floats. # def printDataPoint(tempData,mo,yr): '''tempData is the tempData is the data structure returned by inputTemperatureData. mo is an integer in the range 0 to 11. yr is in the range 0 to len(tempData) - 1. Print the temperature for that month and that year. Suppose mo is 1 and yr is 3. The output should look like this (assuming the value is 65.7): The ave. monthly temperature during Feb in the 4th year is 65.7''' # Do not use a big if - elif - statement! You can use the list # below to print out the correct month name mos = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug','Sept', 'Oct', 'Nov', 'Dec'] def avgTempMarch(tempData): '''Return the average temperature for the month of March as a float for all years in tempData. tempData is the data structure returned by inputTemperatureData.''' def avgTemp(tempData, mo): '''Return the average temperature for month mo as a float for all years in temp_data, where mo is an integer between 0 and 11, representing January to December, respectively. tempData is the data structure returned by inputTemperatureData.''' def higherAvgTemp(tempData, mo1, mo2): '''Return either mo1 or mo2 (integers representing months in the range 0 to 11), whichever has the higher average temperature for all years in tempData. If the months have the same average temperature, then return -1. tempData is the data structure returned by inputTemperatureData.''' def avgTemps (tempData): ''' Return the average of all the temperatures sotred in tempData. tempData is the data structure returned by inputTemperatureData.''' def grade(myAns,correctAnswer): if myAns == correctAnswer: print("Correct, the answer is",myAns) else: print("My answer is",myAns,"but the correct answer is",correctAnswer) def main(): tempData = input_temperature_data("lab6.dat.txt") grade(avgTempMarch(tempData),32.475) grade(avgTemp(tempData,3),46.525) grade(higherAvgTemp(tempData,2,3),3) printDataPoint(tempData,mo=5,yr=2) main()