Difference between revisions of "CSC111 Homework 10 2018"
(Created page with "~~~~ ---- --D. Thiebaut (talk) 17:20, 12 April 2015 (EDT) ---- <bluebox> This Homework is due on Thursday evening, 4/26/18, at 11:...") |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
---- | ---- | ||
− | + | <onlydft> | |
− | |||
− | |||
− | |||
<bluebox> | <bluebox> | ||
Line 25: | Line 22: | ||
==The Weather Data Files == | ==The Weather Data Files == | ||
<br /> | <br /> | ||
− | * The British government has been keeping track of temperatures in several cities of the U.K. The official Web site where the data is available is | + | * The British government has been keeping track of temperatures in several cities of the U.K. The official Web site where the data is available is [https://www.metoffice.gov.uk/public/weather/climate-historic/#?tab=climateHistoric https://www.metoffice.gov.uk/public/weather/climate-historic/#?tab=climateHistoric]. The records of temperatures for '''37''' towns/cities of the UK for the past few years, going back in some cases to 1853, are kept at this site. |
<br /> | <br /> | ||
* The data recorded consists of these quantities | * The data recorded consists of these quantities | ||
Line 59: | Line 56: | ||
* All temperatures should be reported in degrees '''Celsius''' which is the system used in the U.K. (so you do not need to transform the temperatures into Fahrenheit). | * All temperatures should be reported in degrees '''Celsius''' which is the system used in the U.K. (so you do not need to transform the temperatures into Fahrenheit). | ||
<br /> | <br /> | ||
+ | |||
==Questions== | ==Questions== | ||
<br /> | <br /> | ||
Line 79: | Line 77: | ||
Invalid file name, please re-enter | Invalid file name, please re-enter | ||
> armaghdata.txt | > armaghdata.txt | ||
− | 1, - | + | 1, -8.6, 1878, 12 |
2, 23.8, 1995, 8, 1989, 7 | 2, 23.8, 1995, 8, 1989, 7 | ||
3, 256.0, 1940, 6, 252.9, 1949, 6, 251.6, 1935, 5, 244.1, 1957, 6, 243.8, 1989, 7 | 3, 256.0, 1940, 6, 252.9, 1949, 6, 251.6, 1935, 5, 244.1, 1957, 6, 243.8, 1989, 7 | ||
<br /> | <br /> | ||
− | : and here is the expected output for ''' | + | : and here is the expected output for '''ballypatrickdata.txt''': |
> ballypatrickdata.txt | > ballypatrickdata.txt | ||
Line 101: | Line 99: | ||
* Submit your program in the Moodle HW11 PB1 section. You will not be allowed to evaluate this program, so make sure you test it thoroughly. | * Submit your program in the Moodle HW11 PB1 section. You will not be allowed to evaluate this program, so make sure you test it thoroughly. | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | + | </onlydft> | |
<br /> | <br /> | ||
<br /> | <br /> | ||
<!-- ================================================================== --> | <!-- ================================================================== --> | ||
− | <showafterdate after=" | + | <showafterdate after="20180501 12:00" before="20180601 00:00"> |
=Solution Programs= | =Solution Programs= | ||
<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>