UP2
S_ALONE = 0
S_TALKING = 1
# ==============================================================================
# Group class:
# member fields:
# - An array of items, each a Member class
# - A dictionary that keeps who is a chat group
# member functions:
# - join: first time in
# - leave: leave the system, and the group
# - list_my_peers: who is in chatting with me?
# - list_all: who is in the system, and the chat groups
# - connect: connect to a peer in a chat group, and become part of the group
# - disconnect: leave the chat group but stay in the system
# ==============================================================================
class Group:
def __init__(self):
self.members = {}
self.chat_grps = {}
self.grp_ever = 0
def join(self, name):
self.members[name] = S_ALONE
return
def is_member(self, name):
# IMPLEMENTATION
# ---- start your code ---- #
if name in self.members.keys():
return True
return False
# ---- end of your code --- #
# implement
def leave(self, name):
"""
leave the system, and the group
"""
# IMPLEMENTATION
# ---- start your code ---- #
del self.members[name]
for each in self.chat_grps.values():
if name in each:
each.remove(name)
# ---- end of your code --- #
return
def find_group(self, name):
"""
Auxiliary function internal to the class; return two
variables: whether "name" is in a group, and if true
the key to its group
"""
found = False
group_key = 0
# IMPLEMENTATION
# ---- start your code ---- #
for each in self.chat_grps.values():
group_key+=1
if name in each:
found=True
# ---- end of your code --- #
return found, group_key
def connect(self, me, peer):
"""
me is alone, connecting peer.
if peer is in a group, join it
otherwise, create a new group with you and your peer
"""
peer_in_group, group_key = self.find_group(peer)
# IMPLEMENTATION
# ---- start your code ---- #
if peer_in_group is True:
self.chat_grps[group_key].append(me)
self.members[me]=S_TALKING
else:
self.chat_grps[group_key+1]=[me,peer]
self.members[me]=S_TALKING
self.members[peer]=S_TALKING
# ---- end of your code --- #
return
# implement
def disconnect(self, me):
"""
find myself in the group, quit, but stay in the system
"""
# IMPLEMENTATION
# ---- start your code ---- #
for each in self.chat_grps.keys():
if me in self.chat_grps[each]:
self.chat_grps[each].remove(me)
self.members[me]=S_ALONE
if len(self.chat_grps[each])==1:
peer=self.chat_grps[each][0]
self.members[peer]=S_ALONE
self.chat_grps[each].remove(peer)
del self.chat_grps[each]
break
# ---- end of your code --- #
return
def list_all(self):
# a simple minded implementation
full_list = "Users: ------------" + "\n"
full_list += str(self.members) + "\n"
full_list += "Groups: -----------" + "\n"
full_list += str(self.chat_grps) + "\n"
return full_list
# implement
def list_me(self, me):
"""
return a list, "me" followed by other peers in my group
"""
my_list = []
# IMPLEMENTATION
# ---- start your code ---- #
a,b=self.find_group(me)
if a is True:
my_list=self.chat_grps[b]
my_list.remove(me)
my_list.insert(0,me)
# ---- end of your code --- #
return my_list
if __name__ == "__main__":
g = Group()
g.join('Anthony Fauci')
g.join('Boris Johnson')
g.join('Donald Trump')
g.join('Donald Knuth')
print (g.is_member('Anthony Fauci'))
# print(g.list_all())
g.connect('Boris Johnson', 'Donald Trump')
print(g.list_all())
g.connect('Donald Knuth', 'Donald Trump')
print(g.list_all())
g.connect('Anthony Fauci','Donald Trump')
print (g.list_me('Donald Trump'))
print (g.find_group('Boris Johnson'))
g.disconnect('Anthony Fauci')
print (g.list_all())
g.leave('Donald Trump')
print(g.list_all())
g.disconnect('Boris Johnson')
print(g.list_all())