Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Friends by Kisielev
class Friends:
def __init__(self, connections):
self.connections=set(frozenset(con) for con in connections)
#frozen set used to create set of sets, avoiding duplicates
return
def add(self, connection):
if connection in self.connections:
return False
self.connections.add(frozenset(connection))
return True
def remove(self, connection):
try:
self.connections.remove(connection)
return True
except KeyError: #Connection doesn't exist
return False
def names(self): # set off all names within connections
return set( name
for connection in self.connections
for name in connection )
def connected(self, name):
return set( friend
for connection in self.connections
for friend in connection
if (name in connection and friend != name))
May 19, 2018
Comments: