#!/usr/bin/env python # File name: cw1-solution.py # Author Roy Ruddle, Jan 2008 # # Main programme # #filename = 'cw1-results.csv' # name of results file filename = 'cw1-actual-results.csv' # name of results file # # Parameters used to access data columns # TNAME = 0 LEG = TNAME + 1 RNAME = LEG + 1 TIME = RNAME + 1 # # Lists containing name and time of each team # teamName = [] teamTime = [] # # Open results file and read all the lines # fin = open(filename, "r") lines = fin.readlines() numLegs = 3 # No. legs in the relay numTeams = len( lines ) / numLegs # No. teams that took part printed = [] # Used in printing to flag teams that have been processed # # Loop over lines, ignoring first because that's a header # for l1 in range( 1, numLegs * numTeams + 1 ) : columns = lines[l1].split( ',' ) # Extract columns from line # # Find team number # number = -1 # # Loop over teams that have already been stored # for l2 in range( len( teamName ) ) : if columns[TNAME].find( teamName[l2] ) != -1: # Found the team number = l2 # Store team no. break # Break this loop if number == -1 : # Team not yet stored teamName.append( columns[TNAME] ) # Append team name to list teamTime.append( int ( columns[TIME] ) ) # Store time for this leg printed.append( False ) # This team has yet to be printed else : teamTime[number] = teamTime[number] + int( columns[TIME] ) # Add time for this leg # # Parameters used to access columns of output # COL_POSITION = 0 COL_NAME = COL_POSITION + 1 COL_TIME = COL_NAME + 1 # # Print results, in position order # headerName = ['Position', 'Team', 'Time (hh:mm:ss)'] # Column headers for output headerWidth = [0,0,0] # Width of each output column gap = ' ' # Gap between columns of output # # Determine required width of position column # headerWidth[COL_POSITION] = len( headerName[COL_POSITION] ) if len( str( numTeams ) ) > headerWidth[COL_POSITION]: headerWidth[COL_POSITION] = len( str( numTeams ) ) # # Determine required width of team name column # headerWidth[COL_NAME] = len( headerName[COL_NAME] ) for l1 in range( numTeams ) : if len( teamName[l1] ) > headerWidth[COL_NAME] : headerWidth[COL_NAME] = len( teamName[l1] ) # # Time column will be width of its header # headerWidth[COL_TIME] = len( headerName[COL_TIME] ) # # Print headers # for l1 in range( len( headerName ) ) : print headerName[l1].ljust( headerWidth[l1] ), if l1 < len( headerName ) - 1 : print gap, else : print for l1 in range( numTeams ) : quickestTime = -1 quickestTeam = -1 # # Find next quickest team # for l2 in range( len( teamName ) ) : if quickestTime == -1 or teamTime[l2] < quickestTime : quickestTime = teamTime[l2] quickestTeam = l2 # # Print results for next quickest team # print str( l1 + 1 ).rjust( headerWidth[COL_POSITION] ), # Position is right justified print gap, print teamName[quickestTeam].ljust( headerWidth[COL_NAME] ), # Name is left justified print gap, # # Add hours to time string # timeString = '' hours = teamTime[quickestTeam] / 3600 # Integer arithmetic if hours == 0: timeString = '00' elif hours < 10: timeString = '0' timeString += str( hours ) else: timeString = str( hours ) timeString += ':' # # Add minutes to time string # mins = (teamTime[quickestTeam] - hours * 3600) / 60 # Integer arithmetic if mins == 0: timeString += '00' elif mins < 10: timeString += '0' timeString += str( mins ) else: timeString += str( mins ) timeString += ':' # # Add seconds to time string # secs = (teamTime[quickestTeam] - hours * 3600 - mins * 60) if secs == 0: timeString += '00' elif secs < 10: timeString += '0' timeString += str( secs ) else: timeString += str( secs ) #print timeString print timeString.center( headerWidth[COL_TIME] ) # Time is centre justified # # Remove this team from lists # del teamName[quickestTeam:quickestTeam + 1] del teamTime[quickestTeam:quickestTeam + 1] fin.close() # Close the results file