Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Friends by mariano_italino
class Friends:
def __init__(self, connections):
self.connections = connections
self.li = list(connections)
def add(self, connection):
#self.connection = connection
if connection in self.li:
return False
else:
self.li.append(connection)
return True
def remove(self, connection):
#self.connection = connection
if connection in self.li:
self.li.remove(connection)
return True
else:
return False
def names(self):
name =[]
for i,j in self.li:
if i not in name:
name.append(i)
if j not in name:
name.append(j)
return set(name)
def connected(self, name):
l = []
for x,y in self.li:
if name == x:
l.append(y)
elif name == y:
l.append(x)
return set(l)
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. 26, 2016