Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Voice TV Control by mihail.ponomaryov
class VoiceCommand:
def __init__(self, channels):
self._channels = channels
self._current_channel = 0
def first_channel(self):
return self.turn_channel(1)
def last_channel(self):
return self.turn_channel(len(self._channels))
def turn_channel(self, channel_number):
self._current_channel = channel_number - 1
return self.current_channel()
def _previous_next(self, delta):
self._current_channel = (self._current_channel + delta) % len(self._channels)
return self.current_channel()
def next_channel(self):
return self._previous_next(1)
def previous_channel(self):
return self._previous_next(-1)
def current_channel(self):
return self._channels[self._current_channel]
def is_exist(self, channel):
if isinstance(channel, int):
c = 0 <= channel < len(self._channels)
else:
c = channel in self._channels
return ('No', 'Yes')[c]
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!")
July 18, 2018
Comments: