Skip to content

Instantly share code, notes, and snippets.

@eddie-knight
Last active June 28, 2022 17:51
Show Gist options
  • Save eddie-knight/0e5a9cd4131b879f0f115a41f4bea67e to your computer and use it in GitHub Desktop.
Save eddie-knight/0e5a9cd4131b879f0f115a41f4bea67e to your computer and use it in GitHub Desktop.
Python Interview - Microwave

The Microwave

Within one hour, design and implement the behavior of a microwave oven.

It has a numeric keypad and a START button. You are responsible for the logic triggered when any of these buttons is pressed.

  • Pressing a numeric key sends your code one digit.
  • Pressing START tells your code to start "cooking".

This microwave is not fancy. It cannot be stopped or paused once started. Input cannot be cleared. There is no “quick cook” option.

You are not responsible for building a UI or an interactive application. Write functions that might be called as each button is pressed. Use print statements as a substitute for user feedback.

Print what the display will show after each number press. If receiving the button presses for “1”, “3”, and “5”, your code could print "00:01", "00:13", then "01:35".

While cooking, print what the display will show once a second. After “START” is pressed, your code could print “01:34”, “01:33”, “01:32”, and so on.

# solution.py
import time
class MicrowaveOven:
"""
MicrowaveOven handles logic related to cooking
"""
def __init__(self, cli=False):
self.__cli = cli
def cook(self, time):
if self.__cli:
print(f"(Food will cook for {time} seconds.)")
else:
raise AttributeError("CLI set to 'False' but non-CLI features have not yet been implemented")
class MicrowaveRunner:
"""
MicrowaveRunner handles the logic related to user input
"""
def __init__(self, cli=False):
self.__cli = cli
self.__oven = MicrowaveOven(cli)
self.__timer = []
def __str__(self):
return self.screen()
def screen(self) -> str:
output = "".join(self.__timer)
if len(output) == 1:
output = f"00:0{output}"
elif len(output) == 2:
output = f"00:{output}"
elif len(output) == 3:
output = f"0{output[0]}:{output[1:]}"
elif len(output) >= 4:
output = f"{output[0:2]}:{output[2:4]}" # Ignore anything beyond the fourth digit
return output
def input(self, value) -> bool:
""" Adds user input value to timer """
if len(value) > 1:
value = value[1] # Avoid mistaken input beyond a single digit
if len(self.__timer) < 4:
self.__timer.append(value)
return True
else:
if self.__cli:
print("(Bad Beep!\a)")
return False
def convert_time(self) -> int:
screen = self.screen()
minutes = int(screen[0:2])
seconds = int(screen[3:5])
timer = (minutes * 60) + seconds
return timer if timer < 5999 else 5999
def cli_start(self) -> None:
timer = self.convert_time()
while timer:
mins, secs = divmod(timer, 60)
display = '{:02d}:{:02d}'.format(mins, secs)
print(display, end="\n")
time.sleep(1)
timer -= 1
print("Good Beep!\a")
def start(self) -> None:
""" This function has not been implemented """
if self.__cli:
self.cli_start()
else:
raise AttributeError("CLI set to 'False' but non-CLI features have not yet been implemented")
if __name__ == "__main__":
microwave = MicrowaveRunner(cli=True)
start = False
while not start:
value = input("> ")
if value.lower() == "start":
break
microwave.input(value)
print(microwave.screen())
microwave.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment