Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Friends by Froglem95
class Friends(object):
def __init__(self, connections):
self.connections = connections
def add(self, connection):
if connection in self.connections:
return False
else:
self.connections += (connection,)
return True
def remove(self, connection):
if connection in self.connections:
self.connections = (x for x in self.connections if x != connection)
return True
else:
return False
def names(self):
a = set()
for x in self.connections:
a = a.union(x)
return a
def connected(self, name):
a = set()
for x in self.connections:
if name in x:
a = a.union(x)
a.discard(name)
return a
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
letter_friends = Friends(({"a", "b"}, {"b", "c"}, {"c", "a"}, {"a", "c"}))
digit_friends = Friends([{"1", "2"}, {"3", "1"}])
assert letter_friends.add({"c", "d"}) is True, "Add"
assert letter_friends.add({"c", "d"}) is False, "Add again"
assert letter_friends.remove({"c", "d"}) is True, "Remove"
assert digit_friends.remove({"c", "d"}) is False, "Remove non exists"
assert letter_friends.names() == {"a", "b", "c"}, "Names"
assert letter_friends.connected("d") == set(), "Non connected name"
assert letter_friends.connected("a") == {"b", "c"}, "Connected name"
May 11, 2016