Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Friends by Mahoter
class Friends:
def __init__(self, connections):
self.con = [connections[0]]
for a in connections :
e = 0
b = list(a)
for c in self.con:
d = list(c)
if (b[0] != d[0] and b[0] != d[1]) or (b[1] != d[0] and b[1] != d[1]):
e +=1
if e == len(self.con):
self.con += [a]
def add(self, connection):
b = list(connection)
e = 0
for c in self.con:
d = list(c)
if (b[0] != d[0] and b[0] != d[1]) or (b[1] != d[0] and b[1] != d[1]):
e += 1
if e == len(self.con):
self.con += [connection]
return True
return False
def remove(self, connection):
c = list(connection)
for a in self.con:
b=list(a)
if (c[0] == b[0] and c[1] == b[1]) or (c[1] == b[0] and c[0] == b[1]):
self.con.remove(connection)
return True
return False
def names(self):
res = {}
for a in self.con:
b = list(a)
if b[0] not in res:
res[b[0]] = 1
if b[1] not in res:
res[b[1]] = 1
return res.keys()
def connected(self, name):
res = {}
for a in self.con:
b = list(a)
if name == b[0]:
res[b[1]] = 1
if name == b[1]:
res[b[0]] = 1
return res.keys()
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. 17, 2016