Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
While Set Intersections solution in Clear category for How to Find Friends by John.Hammell
def check_connection(network, name1, name2):
# Convert each name pair into a set with name1-name2 pairs split.
network = list(network)
for i in range(0, len(network)):
network[i] = set((network[i]).split("-"))
# Create a new set for names connected with name1.
name1_network = set(name1.split())
# Iterate over network & merge intersecting sets into name1_network
# until there are no more intersections. Check if name2 is in name1_network
# set at the end of each loop.
intersection_exists = True
while intersection_exists:
intersection_exists = False
del_from_network = []
for item in network:
if item.intersection(name1_network):
intersection_exists = True
name1_network = item.union(name1_network)
del_from_network.append(item)
for item in del_from_network:
network.remove(item)
if name2 in name1_network:
return True
return False
Oct. 4, 2015
Comments: