Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Friends by mateusz.miekus
class Friends:
def __init__(self, connections):
self.connections=list(connections)
def add(self, connection):
self.connection=set(connection)
x=list(self.connection)
x=x[::-1]
x=set(x)
if self.connection in self.connections:
if x in self.connections:
return False
else:
self.connections.append(self.connection)
return True
def remove(self, connection):
self.connection=set(connection)
x=list(self.connection)
x=x[::-1]
x=set(x)
if self.connection in self.connections:
self.connections.remove(self.connection)
return True
if x in self.connections:
self.connections.remove(self.connection)
return True
else:
return False
def names(self):
x = set()
y= len(self.connections)-1
while y >= 0:
for i in self.connections[y]:
x.add(i)
y-=1
return x
def connected(self, name):
z=set()
self.name=str(name)
y=len(self.connections)-1
while y>=0:
if self.name in self.connections[y]:
for i in self.connections[y]:
if i!=self.name:
z.add(i)
y-=1
return z
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"
Nov. 4, 2016