Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Friends by Moff
from collections import defaultdict
class Friends(object):
def __init__(self, connections):
self.adj = defaultdict(set)
for connection in connections:
self.add(connection)
def add(self, connection):
a, b = connection
if a in self.adj[b]:
return False
self.adj[a].add(b)
self.adj[b].add(a)
return True
def remove(self, connection):
a, b = connection
if a not in self.adj[b]:
return False
self.adj[a].remove(b)
self.adj[b].remove(a)
return True
def names(self):
return set(k for k, v in self.adj.items() if v)
def connected(self, name):
return self.adj.get(name, set())
July 29, 2015