Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Friends by yukirin
from itertools import chain
class Friends:
def __init__(self, connections):
self.conns = set(map(frozenset, connections))
def add(self, c):
return (True if c not in self.conns else False, self.conns.add(frozenset(c)))[0]
def remove(self, c):
return (True if c in self.conns else False, self.conns.discard(frozenset(c)))[0]
def names(self):
return set(chain.from_iterable(self.conns))
def connected(self, name):
return set(set({name}).symmetric_difference(c).pop() for c in self.conns if name in c)
April 5, 2015