friends.Friends.names()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
__author__ = 'KenMercusLai'
2
3
from itertools import chain
4
5
6
class Friends:
7
    def __init__(self, connections):
8
        self.connections = list(connections)
9
10
    def add(self, connection):
11
        if connection in self.connections:
12
            return False
13
        else:
14
            self.connections.append(connection)
15
            return True
16
17
    def remove(self, connection):
18
        if connection in self.connections:
19
            self.connections.remove(connection)
20
            return True
21
        return False
22
23
    def names(self):
24
        return set(chain(*self.connections))
25
26
    def connected(self, name):
27
        neighbors = []
28
        [neighbors.extend(list(i)) for i in self.connections if name in i]
29
        neighbors = set(neighbors)
30
        if name in set(neighbors):
31
            neighbors.remove(name)
32
        return neighbors
33
34
35
if __name__ == '__main__':
36
    # These "asserts" using only for self-checking
37
    # and not necessary for auto-testing
38
    letter_friends = Friends(({"a", "b"}, {"b", "c"}, {"c", "a"}, {"a", "c"}))
39
    digit_friends = Friends([{"1", "2"}, {"3", "1"}])
40
    assert letter_friends.add({"c", "d"}) is True, "Add"
41
    assert letter_friends.add({"c", "d"}) is False, "Add again"
42
    assert letter_friends.remove({"c", "d"}) is True, "Remove"
43
    assert digit_friends.remove({"c", "d"}) is False, "Remove non exists"
44
    assert letter_friends.names() == {"a", "b", "c"}, "Names"
45
    assert letter_friends.connected("d") == set(), "Non connected name"
46
    assert letter_friends.connected("a") == {"b", "c"}, "Connected name"
47