Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Frozenset and chain solution in Clear category for Friends by Dodge
from itertools import chain
class Friends:
def __init__(self, connections):
self.connections = set(frozenset(c) for c in connections)
def add(self, connection):
not_exist = connection not in self.connections
self.connections.add(frozenset(connection))
return not_exist
def remove(self, connection):
exist = connection in self.connections
self.connections.discard(connection)
return exist
def names(self, connections=None):
return set(chain.from_iterable(connections or self.connections))
def connected(self, name):
return self.names((s for s in self.connections if name in s)) - {name}
Sept. 11, 2015