python-checkio/Github/voice-tv-control.py

56 lines
1.7 KiB
Python
Raw Normal View History

2020-01-03 14:31:18 +01:00
class VoiceCommand:
def __init__(self, channels):
self.channels = channels
self.current = 0
def first_channel(self):
self.current = 0
return self.channels[0]
def last_channel(self):
self.current = len(self.channels) - 1
return self.channels[self.current]
def turn_channel(self, channel):
self.current = channel - 1
return self.current_channel()
def next_channel(self):
self.current += 1
if self.current >= len(self.channels):
self.current = 0
return self.current_channel()
def previous_channel(self):
self.current -= 1
if self.current < 0:
self.current = len(self.channels) - 1
return self.current_channel()
def current_channel(self):
return self.channels[self.current]
def is_exist(self, channel):
if isinstance(channel, int):
return "Yes" if 0 <= channel < len(self.channels) else "No"
else:
return "Yes" if channel in self.channels else "No"
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!")