Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
26 lines (dict as ancestor) solution in Clear category for Voice TV Control by CDG.Axel
class VoiceCommand(dict):
def __init__(self, channels):
super().__init__({i: e for i, e in enumerate(channels, 1)})
self.position = 1
def current_channel(self):
return self[self.position]
def turn_channel(self, position):
self.position = position if position in self else self.position
return self.current_channel()
def first_channel(self):
return self.turn_channel(1)
def last_channel(self):
return self.turn_channel(len(self))
def next_channel(self):
return self.turn_channel(1 + self.position % len(self))
def previous_channel(self):
return self.turn_channel(1 + (self.position - 2) % len(self))
def is_exist(self, channel):
return ('No', 'Yes')[channel in self or channel in self.values()]
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
CHANNELS = ["BBC", "Discovery", "TV1000"]
controller = VoiceCommand(CHANNELS)
assert controller.first_channel() == "BBC"
assert controller.last_channel() == "TV1000"
assert controller.turn_channel(1) == "BBC"
assert controller.next_channel() == "Discovery"
assert controller.previous_channel() == "BBC"
assert controller.current_channel() == "BBC"
assert controller.is_exist(4) == "No"
assert controller.is_exist("TV1000") == "Yes"
print("Coding complete? Let's try tests!")
Oct. 6, 2021
Comments: