Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
mediator design pattern solution in Clear category for Dialogues by Rcp8jzd
VOWELS = 'aeiou'
class Chat:
"""
A chat server to help human and robots
"""
def __init__(self):
self.discussion = []
def connect_human(self, human):
"""
Connects a human to the chat server
:param human: (Human) Instance of the human class
"""
human.mediator = self
def connect_robot(self, robot):
"""
Connects a robot to the chat server
:param robot: (Robot) Instance of the robot class
"""
robot.mediator = self
def show_human_dialogue(self):
"""
Shows the dialog as the human sees it - as simple text
:return: (str) The dialog as simple text
"""
dialogue = ""
for line in self.discussion:
if dialogue:
dialogue = dialogue + "\n"
dialogue = dialogue + f"{line['author']} said: {line['message']}"
return dialogue
def show_robot_dialogue(self):
"""
Shows the dialog as the robot perceives it - as the set of ones and zeroes
To simplify the task, just replace every vowel ('aeiouAEIOU') with "0",
and the rest symbols (consonants, white spaces and special
signs like ",", "!", etc.) with "1".
:return: (str) a set of ones and zeroes
"""
dialogue = ""
for line in self.discussion:
if dialogue:
dialogue = dialogue + "\n"
message = line['message']
translated = ""
for letter in message:
if letter.lower() in VOWELS:
translated = translated + '0'
else:
translated = translated + '1'
dialogue = dialogue + f"{line['author']} said: {translated}"
return dialogue
class Human:
"""
A Human class
"""
def __init__(self, name: str):
self.mediator = None
self.name = name
def send(self, message):
"""
Sends a message to the chat server
:param message: (str) Message to transmit
"""
self.mediator.discussion.append({'author': self.name,
'message': message})
class Robot:
"""
A robot class
"""
def __init__(self, serial_number: str):
self.mediator = None
self.serial_number = serial_number
def send(self, message):
"""
Sends a message to the chat server
:param message: (str) Message to transmit
"""
self.mediator.discussion.append({'author': self.serial_number,
'message': message})
Feb. 25, 2020