-
Notifications
You must be signed in to change notification settings - Fork 0
/
How_to_Find_Friends.py
49 lines (43 loc) · 1.8 KB
/
How_to_Find_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
48
49
# https://py.checkio.org/en/mission/find-friends/
def check_connection(network, drons1, drons2):
network = [elem.split('-') for elem in network]
drones = list(set([x for l in network for x in l]))
drones_connection = []
for index, dron in enumerate(drones):
drones_connection.append([])
for net in network:
elem1, elem2 = net
if dron in net:
drones_connection[index].append(elem1)
drones_connection[index].append(elem2)
l = []
for dron1 in drones_connection:
dron1 = list(set(dron1))
for index, dron in enumerate(drones_connection):
dron = list(set(dron))
if not index:
continue
if set(dron1) & set(dron):
dron1 = list(set(dron1) | set(dron))
if dron1 not in l:
l.append(dron1)
print(l)
for connection in l:
if drons1 in connection and drons2 in connection:
return True
return False
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"scout2", "scout3") == True, "Scout Brotherhood"
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"super", "scout2") == True, "Super Scout"
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"dr101", "sscout") == False, "I don't know any scouts."
print('Done!')