Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Friends by quis20
class Friends:
friends = set()
def __init__(self, connections):
for item in connections:
self.friends.add(frozenset(item))
def add(self, connection):
if connection in self.friends:
return False
else:
self.friends.add(frozenset(connection))
return True
def remove(self, connection):
if connection in self.friends:
self.friends.remove(frozenset(connection))
return True
else:
return False
def names(self):
the_names = set()
for item in self.friends:
the_names.update(item.copy())
return the_names
def connected(self, name):
sum = set()
for item in self.friends:
if name in item:
sum.update(item.copy())
sum = sum.difference(set([name]))
return sum
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"
Jan. 24, 2016