In [24]:
import operator
class TempTracker:
def __init__(self):
self.min = None
self.max = None
self.mean = None
self.mode = None
self.mode_qnt = 0
self.sum = 0.0
self.qnt = 0
self.dict = {}
def insert(self, temperature):
# set min temperature
if self.min is None or self.min > temperature:
self.min = temperature
# set max temperature
if self.max is None or self.max < temperature:
self.max = temperature
# set mean temperature
self.qnt += 1
self.sum += temperature
self.mean = self.sum / self.qnt
# set mode temperature
if temperature in self.dict.keys():
self.dict[temperature] += 1
else:
self.dict[temperature] = 1
if self.mode_qnt < self.dict[temperature]:
self.mode = temperature
def get_max(self):
return self.max
def get_min(self):
return self.min
def get_mean(self):
return self.mean
def get_mode(self):
return self.mode
In [25]:
tracker = TempTracker()
In [26]:
tracker.insert(1)
tracker.insert(2)
tracker.insert(1)
tracker.insert(3)
In [27]:
tracker.get_min()
Out[27]:
In [28]:
tracker.get_max()
Out[28]:
In [29]:
tracker.get_mode()
Out[29]:
In [30]:
tracker.get_mean()
Out[30]: