Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Multicolored Lamp by technick303
class Lamp:
def __init__(self):
self.state = GreenState()
def light(self):
color = self.state.light()
self.state = self.state.next_state()
return color
class GreenState():
def light(self):
return "Green"
def next_state(self):
return RedState()
class RedState():
def light(self):
return "Red"
def next_state(self):
return BlueState()
class BlueState():
def light(self):
return "Blue"
def next_state(self):
return YellowState()
class YellowState():
def light(self):
return "Yellow"
def next_state(self):
return GreenState()
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
lamp_1 = Lamp()
lamp_2 = Lamp()
lamp_1.light() #Green
lamp_1.light() #Red
lamp_2.light() #Green
assert lamp_1.light() == "Blue"
assert lamp_1.light() == "Yellow"
assert lamp_1.light() == "Green"
assert lamp_2.light() == "Red"
assert lamp_2.light() == "Blue"
print("Coding complete? Let's try tests!")
Aug. 28, 2018