Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Voice TV Control by colinmcnicholl
class VoiceCommand:
def __init__(self, channel_names):
self.channel_names = channel_names
self.channel_idx = 0
def __str__(self):
return str(self.channel_names)
def first_channel(self):
self.channel_idx = 0
return self.channel_names[self.channel_idx]
def last_channel(self):
self.channel_idx = -1
return self.channel_names[self.channel_idx]
def turn_channel(self, N):
if N in range(1, len(self.channel_names) + 1):
self.channel_idx = N - 1
return self.channel_names[self.channel_idx]
else:
return "Channel number out of range"
def next_channel(self):
self.channel_idx += 1
return self.channel_names[self.channel_idx % len(self.channel_names)]
def previous_channel(self):
self.channel_idx -= 1
return self.channel_names[self.channel_idx % len(self.channel_names)]
def current_channel(self):
return self.channel_names[self.channel_idx % len(self.channel_names)]
def is_exist(self, a_number_or_a_name):
if (a_number_or_a_name in self.channel_names
or a_number_or_a_name in range(1, len(self.channel_names) + 1)):
return "Yes"
else:
return "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!")
Nov. 23, 2018
Comments: