Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
23-liner: collections.defaultdict(set) does pretty much the job solution in Creative category for Friends by Stensen
class Friends:
def __init__(self, connections):
self.connections = connections
self.data = __import__('collections').defaultdict(set)
for i, j in self.connections:
self.data[i].add(j)
self.data[j].add(i)
def add(self, connection):
a, b = tuple(connection)
if a in self.data[b]: return False
else:
self.data[a].add(b)
self.data[b].add(a)
return True
def remove(self, connection):
a, b = tuple(connection)
if a in self.data[b]:
self.data[a].remove(b)
self.data[b].remove(a)
return True
else: return False
names = lambda self: {name for name, v in self.data.items() if v}
connected = lambda self, name: self.data[name] if name in self.data else set()
April 16, 2021
Comments: