-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfriends.py
47 lines (40 loc) · 1.78 KB
/
friends.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Friends:
def __init__(self, connections):
self.connections = [c for c in connections]
def add(self, connection):
if connection not in self.connections:
self.connections.append(connection)
return True
else:
return False
def remove(self, connection):
if connection in self.connections:
self.connections.remove(connection)
return True
else:
return False
def names(self):
return set([name for c in self.connections for name in c])
def connected(self, name):
ans = []
for c in self.connections:
if name in c:
ans.append(list(c - set([name]))[0])
print(name, c, c - set([name]))
return set(ans)
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"
f = Friends(({"nikola", "sophia"}, {"stephen", "robot"}, {"sophia", "pilot"}))
assert f.connected("nikola") == {'sophia'}
f = Friends(({"nikola", "sophia"}, {"stephen", "robot"}, {"sophia", "pilot"}))
assert f.connected("sophia") == {'pilot', 'nikola'}
print('test all good')