-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhow_to_find_friends.py
36 lines (33 loc) · 1.46 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
'''
We have an array of straight connections between drones. Each connection is represented as a
string with two names of friends separated by hyphen.
For example: "dr101-mr99" means what the dr101 and mr99 are friends.
You should write a function that allow determine more complex connection between drones.
You are given two names also. Try to determine if they are related through common bonds by any depth.
For example: if two drones have a common friends or friends who have common friends and so on.
This concept will help you find not too obvious connections with the building of bond networks.
And how to work social networks.
'''
def check_connection(network, first, second):
'''
Scans network for path between two nodes - first and
second. By "walking" through the network via mutual friends:
1. Node of interest is discovered (True)
2. No path from first to second exists. (False)
'''
relationships = [pair.split('-') for pair in network]
visited = [first]
in_path = []
# Finds neighbor relationships of initial node
for conn in relationships:
if first in conn:
in_path.append(conn)
# Search neighbors until second is located, or terminate.
for pair in in_path:
if second in pair:
return True
for conn in relationships:
if any(node in conn for node in pair):
in_path.append(conn)
relationships.remove(conn)
return False