|
1
|
|
|
def check_connection(network, first, second): |
|
2
|
|
|
# parse given connections to groups |
|
3
|
|
|
connection_groups = [] |
|
4
|
|
|
all_people = [] |
|
5
|
|
|
for i in network: |
|
6
|
|
|
x, y = i.split('-') |
|
7
|
|
|
all_people += [x, y] |
|
8
|
|
|
# check if x/y in a group, add them to the group |
|
9
|
|
|
# or create a new group |
|
10
|
|
|
existed = False |
|
11
|
|
|
for j in connection_groups: |
|
12
|
|
|
if x in j or y in j: |
|
13
|
|
|
j += [x, y] |
|
14
|
|
|
existed = True |
|
15
|
|
|
if not existed: |
|
16
|
|
|
connection_groups.append([x, y]) |
|
17
|
|
|
|
|
18
|
|
|
# remove dup items in groups |
|
19
|
|
|
connection_groups = [set(i) for i in connection_groups] |
|
20
|
|
|
all_people = set(all_people) |
|
21
|
|
|
|
|
22
|
|
|
# i don't think this merge codes are good, but it works at least |
|
23
|
|
|
# in case there may be shared items between groups, we have to merge them |
|
24
|
|
|
for i in all_people: |
|
25
|
|
|
# get indexes of groups that shares people |
|
26
|
|
|
# generate a reversed index to avoid issue when removing item from list |
|
27
|
|
|
index_list = [j for j in range(len(connection_groups) - 1, -1, -1) |
|
28
|
|
|
if i in connection_groups[j]] |
|
29
|
|
|
|
|
30
|
|
|
if len(index_list) > 1: |
|
31
|
|
|
temp = [] |
|
32
|
|
|
for j in index_list: |
|
33
|
|
|
temp += list(connection_groups[j]) |
|
34
|
|
|
del connection_groups[j] |
|
35
|
|
|
connection_groups.append(set(temp)) |
|
36
|
|
|
|
|
37
|
|
|
# if first & second are in the same group, return True |
|
38
|
|
|
for i in connection_groups: |
|
39
|
|
|
if first in i and second in i: |
|
40
|
|
|
return True |
|
41
|
|
|
return False |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
if __name__ == '__main__': # pragma: no cover |
|
45
|
|
|
# These "asserts" using only for self-checking and not necessary for |
|
46
|
|
|
# auto-testing |
|
47
|
|
|
assert check_connection( |
|
48
|
|
|
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2", |
|
49
|
|
|
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"), |
|
50
|
|
|
"scout2", "scout3") is True, "Scout Brotherhood" |
|
51
|
|
|
assert check_connection( |
|
52
|
|
|
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2", |
|
53
|
|
|
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"), |
|
54
|
|
|
"super", "scout2") is True, "Super Scout" |
|
55
|
|
|
assert check_connection( |
|
56
|
|
|
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2", |
|
57
|
|
|
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"), |
|
58
|
|
|
"dr101", "sscout") is False, "I don't know any scouts." |
|
59
|
|
|
|