Difference between revisions of "CSC111 Homework 10 2018"
Line 2: | Line 2: | ||
---- | ---- | ||
− | + | <onlydft> | |
<bluebox> | <bluebox> | ||
Line 151: | Line 151: | ||
<br /> | <br /> | ||
− | + | </onlydft> | |
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 13:46, 1 June 2018
D. Thiebaut (talk) 20:49, 15 April 2018 (EDT)
<showafterdate after="20180501 12:00" before="20180601 00:00">
Solution Programs
Problem 1
# hw11_1.py
# D. Thiebaut
# This program prompts the user for a file name that contains data about
# weather conditions recording a given month in a British city. The source
# of the data is taken from http://www.metoffice.gov.uk/climate/uk/stationdata/
# This program outputs several quantities, including the years and months when
# the coldest temperature was recorded, the year the warmest temperature was recorded,
# the 5 sunniest months and years.
def getData():
#return open( "armagh.txt", "r" ).read()
fileName = input( "> " )
while True:
try:
text = open( fileName, "r" ).read()
return text
except:
print( "Invalid file name. Reenter" )
fileName = input( "> " )
def main():
text = getData()
lines = text.split( "\n" )
# create a list of just year, month, minT, maxT, rain, and sun
list = []
for line in lines:
line = line.strip()
if len( line ) == 0:
continue
# skip line that do not start with a year
if not line[0] in ['1', '2']:
continue
# remove stars and "Provisional" and split
fields = line.replace( '*', '' ).replace( 'Provisional', '' ).split( )
# split line into variables
if len( fields ) == 7:
year, month, maxT, minT, af, rain, sun = fields
else:
continue
# create tuple and record it in list
tuple = ( year, month, maxT, minT, rain, sun )
list.append( tuple )
#===============================================================
# Find coldest months and years on record
#===============================================================
# create a new list, putting min temperature first
listMinTemp = []
for year, month, maxT, minT, rain, sun in list:
if minT=="---":
continue
listMinTemp.append( ( float(minT), year, month ) )
# sort from coldest to warmest min temp
listMinTemp.sort()
# find coldest temperature
coldest = listMinTemp[0][0]
# find all months and years with that coldest temperature
print( "1, ", coldest, end="", sep="" )
for minT, year, month in listMinTemp:
if minT == coldest:
print( ", ", year, ", ", month, end="", sep="" )
print()
#===============================================================
# Find warmest months and years on record
#===============================================================
# create a new list, putting min temperature first
listMaxTemp = []
for year, month, maxT, minT, rain, sun in list:
if maxT=="---":
continue
listMaxTemp.append( ( float(maxT), year, month ) )
# sort from coldest to warmest min temp
listMaxTemp.sort()
listMaxTemp.reverse()
# find coldest temperature
warmest = listMaxTemp[0][0]
# find all months and years with that coldest temperature
print( "2, ", warmest, end="", sep="" )
for maxT, year, month in listMaxTemp:
if maxT == warmest:
print( ", ", year, ", ", month, end="", sep="" )
print()
#===============================================================
# Find the 5 sunniest months, listed in order of sun exposure
#===============================================================
# create a new list, putting min temperature first
sunniestMonthsYears = []
for year, month, maxT, minT, rain, sun in list:
if sun=="---":
continue
sun = float( sun )
sunniestMonthsYears.append( (sun, year, month) )
# sort from coldest to warmest min temp
sunniestMonthsYears.sort()
sunniestMonthsYears.reverse()
# find all months and years with that coldest temperature
print( "3", end="" )
for i in range( min( 5, len( sunniestMonthsYears) ) ):
sun, year, month = sunniestMonthsYears[i]
print( ", {0:1.1f}, {1:1}, {2:1}".format( sun, year, month), end="", sep="" )
print()
if __name__=="__main__":
main()
</showafterdate>