Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Friends by TovarischZhukov
class Friends:
def __init__(self, connections):
self.elements = {",".join (sorted(list(val))):1 for val in list(connections)}
def add(self, new_elem):
new_elem = sorted(list(new_elem))
for val in self.elements:
if new_elem== val.split(","):
return False
self.elements[",".join (new_elem)]=1
return True
def remove(self, del_elem):
del_elem = sorted(list(del_elem))
for val in self.elements:
if del_elem == val.split(","):
del self.elements[val]
return True
return False
def names(self):
return {val for elem in self.elements for val in elem.split(",")}
def connected(self, name):
return {elem.split(",")[0] if name == elem.split(",")[1] else elem.split(",")[1] for elem in self.elements if name in elem.split(",")}
Dec. 3, 2015