#---------------------------------------------- # PROBLEM 1 #---------------------------------------------- # # Download the file data.log from: # # http:/www.samyzaf.com/braude/PYTHON/projects/data.log # # This file contains time/temparature data as sampled by a thermostat in a sensor unit in one day. # Write Python code for answering the following questions: # (a) What are the minimal and maximal temperatures ? # (b) In what times the minimal temperature was obtained? # (c) In what times the maximal temperature was obtained? # (d) What is the average temperature? # # Hint: # Your code should produce the following output: # Minimal temperature = 17.33 # Maximal temperature = 46.5 # Min times = ['01:51:13', '02:49:36', '19:09:31'] # Max times = ['03:39:20', '07:06:30', '11:20:18', '15:21:09'] # Average temperature = 32.0354964895 #---------------------------------------------- # SOLUTION: #---------------------------------------------- def solution_1(): # build a dictionary Tdict that maps time to temperature # key=time, value=temperature. key/value pairs are read from the file 'data.log' Tdict = dict() f = open('data.log', 'r') for line in f: time, temperature = line.split() # read what the split() function does to a string Tdict[time] = float(temperature) # we must convert temperature string to float ! f.close() min_temperature = min(Tdict.values()) max_temperature = max(Tdict.values()) print "Minimal temperature =", min_temperature print "Maximal temperature =", max_temperature min_times = [] max_times = [] for time in Tdict.keys(): if Tdict[time] == min_temperature: min_times.append(time) if Tdict[time] == max_temperature: max_times.append(time) print "Min times =", min_times print "Max times =", max_times print "Average temperature =", sum(Tdict.values()) / len(Tdict) if __name__ == "__main__": solution_1()