Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Microwave Ovens by PythonLearner
class MicrowaveTime(object):
def __init__(self, minutes=0, seconds=0):
self.minute_duration = 60
self.max_minutes = 90
self.min_time = 0
self.minutes = minutes
self.seconds = seconds
self.validate()
def __str__(self):
minutes = str(self.minutes).zfill(2)
seconds = str(self.seconds).zfill(2)
return "{0}:{1}".format(minutes, seconds)
def validate(self):
self.minutes = max(self.min_time, self.minutes)
self.seconds = max(self.min_time, self.seconds)
minutes, seconds = divmod(self.seconds, self.minute_duration)
self.seconds = seconds
self.minutes += minutes
if self.minutes > self.max_minutes:
self.minutes = self.max_minutes
self.seconds = self.min_time
def add(self, minutes, seconds):
self.minutes += minutes
self.seconds += seconds
self.validate()
def subtract(self, minutes, seconds):
self.minutes -= minutes
self.seconds -= seconds
self.validate()
class MicrowaveBase(object):
def __init__(self, time_class=MicrowaveTime):
self.time_class = time_class
self.time = self.time_class()
self.second_marker = 's'
self.minut_marker = 'm'
def set_time(self, time):
minutes, seconds = map(int, time.split(':'))
self.time = self.time_class(minutes, seconds)
def parse_time(self, time):
marker = time[-1]
number = int(time[:-1])
if time[-1] == self.second_marker:
minutes_seconds = divmod(number, self.time.minute_duration)
elif time[-1] == self.minut_marker:
minutes_seconds = (number, 0)
return minutes_seconds
def add_time(self, time):
minutes, seconds = self.parse_time(time)
self.time.add(minutes, seconds)
def del_time(self, time):
minutes, seconds = self.parse_time(time)
self.time.subtract(minutes, seconds)
class Microwave1(MicrowaveBase):
def show_time(self):
time = str(self.time)
return "_{0}".format(time[1:])
class Microwave2(MicrowaveBase):
def show_time(self):
time = str(self.time)
return "{0}_".format(time[:-1])
class Microwave3(MicrowaveBase):
def show_time(self):
return str(self.time)
class RemoteControl(object):
def __init__(self, microwave):
self.microwave = microwave
def set_time(self, time):
self.microwave.set_time(time)
def add_time(self, time):
self.microwave.add_time(time)
def del_time(self, time):
self.microwave.del_time(time)
def show_time(self):
return self.microwave.show_time()
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
microwave_1 = Microwave1()
microwave_2 = Microwave2()
microwave_3 = Microwave3()
remote_control_1 = RemoteControl(microwave_1)
remote_control_1.set_time("01:00")
remote_control_2 = RemoteControl(microwave_2)
remote_control_2.add_time("90s")
remote_control_3 = RemoteControl(microwave_3)
remote_control_3.del_time("300s")
remote_control_3.add_time("100s")
assert remote_control_1.show_time() == "_1:00"
assert remote_control_2.show_time() == "01:3_"
assert remote_control_3.show_time() == "01:40"
print("Coding complete? Let's try tests!")
Sept. 4, 2018