Difference between revisions of "CSC111 Homework 8 Solution Programs 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Program 1= <br /> <source lang="python"> # hw8a.py # Arcadia Kratkiewicz (ap) # Marianne Staknis (au) # 3/30/2014 # # a modified version of a simple eliza-like pr...")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 10:41, 12 April 2014 (EDT)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 10:41, 12 April 2014 (EDT)
 
----
 
----
 +
<onlydft>
 +
 
=Program 1=
 
=Program 1=
 
<br />
 
<br />
Line 211: Line 213:
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
 +
# hw8b.py
 +
# Youyou Tian (bm)
 +
# (slightly edited by D. Thiebaut)
 +
# 4/2/14
 +
#
 +
# This program expects data stored in the file; the data comes from this
 +
# URL: http://www.metoffice.gov.uk/climate/uk/stationdata/
 +
# where temperatures in the UK have been recorded over the past 200 years or so.
 +
# The format for each line is the following:
 +
# year
 +
# month of the year (1 for January, 2 for February, etc.)
 +
# max temperature that month (in Celcius)
 +
# min temperature that month
 +
# number of days of air-frost
 +
# amount of rain (millimeter)
 +
# number of sun hours that month
 +
#
 +
# The original data files can be found on this site. The following legend appears on all the data files:
 +
# Estimated data is marked with a * after the value. Missing data (more than 2 days missing in month)
 +
#  is marked by ---. Sunshine data taken from an automatic Kipp & Zonen sensor marked with a #,
 +
# otherwise sunshine data taken from a Campbell Stokes recorder.'
 +
#
 +
# Given the temperatures data, this program:
 +
# finds the coldest temperature for the year recorded,
 +
# the hottest temperature recorded,
 +
# the coldest city in December 2011
 +
# and the largest difference between temperatures
 +
 +
from temp import temperatures
 +
 +
tempList = temperatures.split("\n") #Splits the temperature table into lists
 +
 +
#
 +
# findColdestTemp(list temperatures)
 +
# finds the coldest temperature, with the city, year and month
 +
#
 +
def findColdestTemp(temperatures):
 +
    tempList = []
 +
    for temp in temperatures:
 +
        tempList.append(getColdestTempList(temp))
 +
    return min(tempList)
 +
 +
#
 +
# getColdestTemp(list temperatures)
 +
# reorders the elements in temp list so that it is
 +
# [tempMin, year, month, city]
 +
 +
def getColdestTempList(temperatures):
 +
    temp = temperatures.split()
 +
    city = temp[0]
 +
    year = int(temp[1])
 +
    month = int(temp[2])
 +
    tempMin = float(temp[4].rstrip("*-"))
 +
    return[tempMin, year, month, city]
 +
 +
#
 +
# findHottestTemp(list temperatures)
 +
# finds the hottest temperature, with the city, year and month
 +
#
 +
def findHottestTemp(temperatures):
 +
    tempList = []
 +
    for temp in temperatures:
 +
        tempList.append(getHottestTempList(temp))
 +
    return max(tempList)
 +
 +
#
 +
# getColdestTemp(list temperatures)
 +
# reorders the elements in temp list so that it is
 +
# [tempMax, year, month, city]
 +
 +
def getHottestTempList(temperatures):
 +
    temp = temperatures.split()
 +
    city = temp[0]
 +
    year = int(temp[1])
 +
    month = int(temp[2])
 +
    tempMax = float(temp[3].rstrip("*-"))
 +
    return[tempMax, year, month, city]
 +
 +
#
 +
# findColdestCity(list temperatures, int year)
 +
# finds the coldest city in december given a year
 +
 +
def findColdestCity(temperatures, year):
 +
    tempList = []
 +
    for temp in temperatures:
 +
        returnedTemp = getColdestTempList(temp)
 +
        if returnedTemp[1] == year and returnedTemp[2] == 12:
 +
            tempList.append(returnedTemp)   
 +
    return min(tempList)
 +
 +
#
 +
# findLargestSwing(list temperatures)
 +
# finds the largest difference of temperatures, with the year
 +
# month and city
 +
 +
def findLargestSwing(temperatures):
 +
    tempList = []
 +
    for temp in temperatures:
 +
        tempList.append(getSwingTemp(temp))
 +
    return max(tempList)
 +
 +
 +
#
 +
# getSwingTemp(list temperatures)
 +
# reorders the elements in temp list so that it is
 +
# [absoulte difference between minTemp and maxTemp, year, month, city]
 +
 +
def getSwingTemp(temperatures):
 +
    temp = temperatures.split()
 +
    city = temp[0]
 +
    year = int(temp[1])
 +
    month = int(temp[2])
 +
    tempMin = float(temp[4].rstrip("*-"))
 +
    tempMax = float(temp[3].rstrip("*-"))
 +
    return[abs(tempMin-tempMax), year, month, city]
 +
 +
#
 +
# main()
 +
#
 +
def main():
 +
    #answer to question 1
 +
    coldest = findColdestTemp(tempList)
 +
    print("The coldest temperature recorded is %.1fC in %d/%d, in %s."
 +
            % (coldest[0], coldest[2], coldest[1], coldest[3]))
 +
 +
    #answer to question 2
 +
    hottest = findHottestTemp(tempList)
 +
    print("The hottest temperature recorded is %.1fC in %d/%d, in %s."
 +
            % (hottest[0], hottest[2],hottest[1], hottest[3]))
 +
 +
    #answer to question 3
 +
    coldestCity = findColdestCity(tempList, 2011)
 +
    print("The coldest city in 12/%d is %s at a temperature of %.1fC."
 +
          % (coldestCity[1], coldestCity[3], coldestCity[0]))
 +
   
 +
    #answer to question 4
 +
    diff = findLargestSwing(tempList)
 +
    print("%s saw the largest temperature difference of %.1fC in %d/%d."
 +
          % (diff[3], diff[0], diff[2], diff[1]))
 +
 +
main()
  
 +
 
</source>
 
</source>
 +
</onlydft>
  
 
<br />
 
<br />

Latest revision as of 20:42, 9 January 2015

--D. Thiebaut (talk) 10:41, 12 April 2014 (EDT)



...