Difference between revisions of "CSC111 Homework 10 2018"

From dftwiki3
Jump to: navigation, search
(The Weather Data Files)
 
(7 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
----
 
----
  
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 17:20, 12 April 2015 (EDT)
+
<onlydft>
----
 
 
 
 
 
  
 
<bluebox>
 
<bluebox>
Line 80: Line 77:
 
  Invalid file name, please re-enter
 
  Invalid file name, please re-enter
 
  > armaghdata.txt
 
  > armaghdata.txt
  1, -4.2, 1878, 12, 1895, 2
+
  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 '''ballpatrickdata.txt''':
+
: and here is the expected output for '''ballypatrickdata.txt''':
 
   
 
   
 
  > ballypatrickdata.txt
 
  > ballypatrickdata.txt
Line 102: 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="20180422 12:00" before="20180601 00:00">
+
<showafterdate after="20180501 12:00" before="20180601 00:00">
 
=Solution Programs=
 
=Solution Programs=
 
<br />
 
<br />

Latest revision as of 12: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>