Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Microwave Ovens by boxdima1
class MicrowaveBase:
def __init__(self):
self._time = "00:00"
@property
def time(self):
return self._time
@time.setter
def time(self, seconds):
seconds = min(90 * 60, max(0, seconds))
minutes, seconds = seconds // 60, seconds % 60
self._time = f"{minutes:02}:{seconds:02}"
def display(self):
return self.time
class Microwave1(MicrowaveBase):
def display(self):
return "_" + self.time[1:]
class Microwave2(MicrowaveBase):
def display(self):
return self.time[:-1] + "_"
class Microwave3(MicrowaveBase):
pass
class RemoteControl:
def __init__(self, microwave: MicrowaveBase):
self.microwave = microwave
def set_time(self, time):
minutes, seconds = time.split(":")
self.microwave.time = int(seconds) + int(minutes) * 60
def add_time(self, time):
minutes, seconds = self.microwave.time.split(":")
seconds = int(seconds) + int(minutes) * 60
if time[-1] == "s":
add_min, add_sec = int(time[:-1]) // 60, int(time[:-1]) % 60
seconds += add_sec + add_min * 60
elif time[-1] == "m":
seconds += int(time[:-1]) * 60
self.microwave.time = seconds
def del_time(self, time):
minutes, seconds = self.microwave.time.split(":")
seconds = int(seconds) + int(minutes) * 60
if time[-1] == "s":
del_min, del_sec = int(time[:-1]) // 60, int(time[:-1]) % 60
seconds = seconds - del_sec - del_min * 60
elif time[-1] == "m":
seconds -= int(time[:-1]) * 60
self.microwave.time = seconds
def show_time(self):
return self.microwave.display()
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!")
Feb. 10, 2021