Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Friends by slygoblin
class Friends:
def __init__(self, connections):
# raise NotImplementedError
self.connections = []
for c in connections:
sorted(c)
for c in connections:
if self.connections and not self.connections.count(c) == 1:
self.connections.append(c)
else:
self.connections.append(c)
def add(self, connection):
# raise NotImplementedError
sorted(connection)
if self.connections.count(connection) == 1:
return False
else:
self.connections.append(connection)
return True
def remove(self, connection):
# raise NotImplementedError
sorted(connection)
if self.connections.count(connection) == 1:
self.connections.remove(connection)
return True
else:
return False
def names(self):
# raise NotImplementedError
result = set()
for i in range(len(self.connections)):
for j in self.connections[i]:
if not self.connections.count(j) == 1:
result.add(j)
return result
def connected(self, name):
# raise NotImplementedError
result = set()
for i in range(len(self.connections)):
if name in self.connections[i]:
for j in self.connections[i]:
if name != j:
result.add(j)
return result
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"
Dec. 9, 2015