Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Microwave Ovens by keromage
import datetime
class MicrowaveBase:
def __init__(self):
self.time = 0
def check_time(self):
if self.time < 0:
self.time = 0
if self.time > 5400: # 90minutes
self.time = 5400
def screen_structure(self):
minutes, seconds = divmod(self.time, 60)
minutes = str(minutes).zfill(2)
seconds = str(seconds).zfill(2)
screen = ":".join([minutes, seconds])
return screen
def show_time(self):
self.check_time()
return self.screen_structure()
class Microwave3(MicrowaveBase):
def __init__(self):
MicrowaveBase.__init__(self)
class Microwave1(MicrowaveBase):
def __init__(self):
MicrowaveBase.__init__(self)
def screen_structure(self):
MicrowaveBase.screen_structure(self)
minutes, seconds = divmod(self.time, 60)
minutes = str(minutes).zfill(2)
minutes = "_" + minutes[1]
seconds = str(seconds).zfill(2)
screen = ":".join([minutes, seconds])
return screen
class Microwave2(MicrowaveBase):
def __init__(self):
MicrowaveBase.__init__(self)
def screen_structure(self):
MicrowaveBase.screen_structure(self)
minutes, seconds = divmod(self.time, 60)
minutes = str(minutes).zfill(2)
seconds = str(seconds).zfill(2)
seconds = seconds[0] + "_"
screen = ":".join([minutes, seconds])
return screen
class RemoteControl:
def __init__(self, microwave):
self.microwave = microwave
def set_time(self, time):
minutes, seconds = time.split(":")
minutes = int(minutes)
seconds = int(seconds)
td = datetime.timedelta(minutes=minutes, seconds=seconds).seconds
self.microwave.time = td
def show_time(self):
return self.microwave.show_time()
def add_time(self, time):
unit = {"m": 60, "s": 1}
s = int(time[:-1]) * unit[time[-1]]
self.microwave.time += s
self.microwave.check_time()
def del_time(self, time):
unit = {"m": 60, "s": 1}
s = int(time[:-1]) * unit[time[-1]]
self.microwave.time -= s
self.microwave.check_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!")
Nov. 13, 2019