Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.2 kB
3
Indexable
Never
#!/usr/bin/env python                                                                                                                                                                         
from operator import itemgetter
import sys
from datetime import datetime, time
# need for 3 worst hvac systems                                                                                                                                                               
hvac = {}
# need for 3 hottest buildings                                                                                                                                                                
bldgs = {}
# need for time-of-day                                                                                                                                                                        
bldgTimes = {}
# need for avg temperature                                                                                                                                                                    
bldgTemps = {}
# must partition line                                                                                                                                                                         
for line in sys.stdin:
    line = line.strip()
    date, time, targetTemp, actualTemp, system, systemAge, buildingID = line.split('\t')
    # find the difference between target and actual temperature                                                                                                                               
    if system in hvac:
        hvac[system].append(abs(float(targetTemp)-float(actualTemp)))
    else:
        hvac[system] = []
        hvac[system].append(abs(float(targetTemp)-float(actualTemp)))
    # now partition during NORMAL business hours, in this case, 9:00 am to 5:00 pm (17:00:00 military)                                                                                        
    if buildingID in bldgs:
        workHrs = datetime.strptime(time, '%H:%M:%S')
        beginTime = datetime.strptime("9:00:00", '%H:%M:%S')
        endTime = datetime.strptime("17:00:00", '%H:%M:%S')
        if workHrs >= beginTime and workHrs <= endTime:
            bldgs[buildingID].append(float(actualTemp))
            bldgTimes[buildingID].append(time)
            bldgTemps[buildingID].append(float(actualTemp))
    else:
        workHrs = datetime.strptime(time, '%H:%M:%S')
        beginTime = datetime.strptime("9:00:00", '%H:%M:%S')
        endTime = datetime.strptime("17:00:00", '%H:%M:%S')
        if workHrs >= beginTime and workHrs <= endTime:
            bldgs[buildingID] = []
            bldgs[buildingID].append(float(actualTemp))
            bldgTimes[buildingID] = []
            bldgTemps[buildingID] = []
            bldgTimes[buildingID].append(time)
            bldgTemps[buildingID].append(float(actualTemp))
# Reduce                                                                                                                                                                                      
print 'Sys\tAvg. Temperature Difference'
# for each sytem in hvac, print the system and the average temperature difference                                                                                                             
for system in hvac.keys():
    tempDiffAvg = sum(hvac[system])*1.0 / len(hvac[system])
    print '%s\t%s' % (system, tempDiffAvg)
    hvac[system] = []
    hvac[system].append(tempDiffAvg)
# for each system in hvac, print the 3 worst systems and the temperature difference                                                                                                           
hvac_keys = sorted(hvac, key=hvac.get, reverse=True)[:3]
print '\nThe 3 worst HVAC systems, based on all available data:'
print 'Sys\tAverage Temperature Difference'
for system in hvac_keys:
    print '%s\t%s' % (system, hvac[system][0])

# Average Temperature for buildings and sort                                                                                                                                                  
print '\nBldg\tAvg. Temperature'
for buildingID in bldgs.keys():
    tempAvg = sum(bldgs[buildingID])*1.0 / len(bldgs[buildingID])
    print '%s\t%s' % (buildingID, tempAvg)
    bldgs[buildingID] = []
    bldgs[buildingID].append(tempAvg)

bldgs_keys = sorted(bldgs, key=bldgs.get, reverse=True)[:3]
print '\nThe 3 hottest buildings, based on all available data:'
print 'Bldg\tAverage Temperature'
for buildingID in bldgs_keys:
    print '%s\t%s' % (buildingID, bldgs[buildingID][0])

# For the 3 hottest buildings, based on available data print building, time, and temp                                                                                                         
print '\nHottest Three Building\'s Recorded Times & Temperatures:'
print 'Bldg\tTimes\tTemperatures'
for buildingID in bldgs_keys:
    for x in range(len(bldgTimes[buildingID])):
        if x == 0:
            print '%s\t%s\t%s' % (buildingID, bldgTimes[buildingID][x], bldgTemps[buildingID][x])
        else:
            print '%s\t%s\t%s' % ('', bldgTimes[buildingID][x], bldgTemps[buildingID][x])