''' weatherstats.py Ben Cochran This is my implementation of the weather stats assignment. ''' import sys, math def standardDeviation(listOfNumbers): '''Returns the standard deviation of the specified list of numbers if the list contains 2 or more elements. Otherwise, returns 0. This function assumes that its parameter refers to a (possibly empty) list of numbers.''' if len(listOfNumbers) == 0: return 0 theMean = mean(listOfNumbers) squaresList = [] for item in listOfNumbers: theSquare = item ** 2 squaresList.append(theSquare) return math.sqrt(sum(squaresList) / float(len(squaresList))) def mean(listOfNumbers): '''Returns the mean of the specified list of numbers if the list contains at least one element. Otherwise, returns 0. This function assumes that its parameter refers to a (possibly empty) list of numbers.''' if len(listOfNumbers) == 0: return 0 theSum = sum(listOfNumbers) theMean = theSum / float(len(listOfNumbers)) return theMean def loadWeatherData(fileName): '''Loads temperature data from the specified file into a list of lists of integers, and returns the list. This function assumes that each line of the file consists of a year followed by some number of temperatures, all separated by commas. For example, if the file looks like this: 1939,-10,5,3,14,... 1940,2,8,-1,4,... ... 2008,7,12,3,5,-2,... (which means that on Jan 1, 1939, the high temperature was 10, etc.), then loadWeatherData will return a list of lists like this: [[1939,-10,5,3,14,...], [1940,2,...], ..., [2008,7,12,...]] ''' openFile = open(fileName) allData = [] for line in openFile: yearData = [] yearDataAsStrings = line.split(',') for item in yearDataAsStrings: yearData.append(int(item)) allData.append(yearData) openFile.close() return allData def main(args): if len(args) != 2: print 'usage: python %s weather_file' % args[0] sys.exit() filename = args[1] data = loadWeatherData(filename) # The way I'm keeping track of maximums and minimums is to first set the # value as `None` then use `or highestMean == None` in my comparisons highestMean = None highestMeanYear = None lowestMean = None lowestMeanYear = None highestStdDev = None highestStdDevYear = None lowestStdDev = None lowestStdDevYear = None highestTemp = None highestTempYear = None lowestTemp = None lowestTempYear = None print "High temperature data (Fahrenheit)" print print "Year Mean Std Dev" for yearData in data: year = yearData[0] temps = yearData[1:] theMean = mean(temps) stdDev = standardDeviation(temps) highest = max(temps) lowest = min(temps) print "%s%10.2f%10.2f" % (year, theMean, stdDev) if theMean > highestMean or highestMean == None: highestMean = theMean highestMeanYear = year if theMean < lowestMean or lowestMean == None: lowestMean = theMean lowestMeanYear = year if stdDev > highestStdDev or highestStdDev == None: highestStdDev = stdDev highestStdDevYear = year if stdDev < lowestStdDev or lowestStdDev == None: lowestStdDev = stdDev lowestStdDevYear = year if highest > highestTemp or highestTemp == None: highestTemp = highest highestTempYear = year if lowest < lowestTemp or lowestTemp == None: lowestTemp = lowest lowestTempYear = year print # empty line print 'Highest mean temperature: %.2f (%s)' % (highestMean, highestMeanYear) print 'Lowest mean temperature: %.2f (%s)' % (lowestMean, lowestMeanYear) print 'Highest standard deviation: %.2f (%s)' % (highestStdDev, highestStdDevYear) print 'Lowest standard deviation: %.2f (%s)' % (lowestStdDev, lowestStdDevYear) print 'Highest temperature: %.2f (%s)' % (highestTemp, highestTempYear) print 'Lowest temperature: %.2f (%s)' % (lowestTemp, lowestTempYear) main(sys.argv)