Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Dialogues by Kurush
VOWELS = "aeiou"
class Chat:
def __init__(self):
self.text = []
def connect_human(self, human):
human.chat = self
def connect_robot(self, bot):
bot.chat = self
def show_human_dialogue(self):
dialogue = "\n".join([name + " said: " + text for (name, text) in self.text])
return dialogue
def show_robot_dialogue(self):
dialogue = "\n".join([name + " said: " + self.robot_transform(text) for (name, text) in self.text])
return dialogue
def robot_transform(self, text):
return "".join(map(lambda char: "0" if char in VOWELS else "1", text))
def add_text(self, sender, text):
self.text.append((sender.name, text))
class Human:
def __init__(self, name):
self.name = name
self.chat = None
def send(self, text):
self.chat.add_text(self, text)
class Robot:
def __init__(self, name):
self.name = name
self.chat = None
def send(self, text):
self.chat.add_text(self, text)
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
chat = Chat()
karl = Human("Karl")
bot = Robot("R2D2")
chat.connect_human(karl)
chat.connect_robot(bot)
karl.send("Hi! What's new?")
bot.send("Hello, human. Could we speak later about it?")
assert chat.show_human_dialogue() == """Karl said: Hi! What's new?
R2D2 said: Hello, human. Could we speak later about it?"""
assert chat.show_robot_dialogue() == """Karl said: 101111011111011
R2D2 said: 10110111010111100111101110011101011010011011"""
print("Coding complete? Let's try tests!")
Feb. 22, 2019