Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for How to Find Friends by MMM_AAA_NNN
from itertools import combinations
def check_connection(network, first, second):
networklist = list(network)
collectives = []
while networklist:
collectives += [frozenset(networklist.pop().split("-"))]
stop = False
while not stop:
oldcollectives = set(collectives)
newcollectives = []
for combo in combinations(collectives, 2):
if combo[0] & combo[1] and combo[0] != combo[1]:
newcollectives.append(combo[0] | combo[1])
if combo[0] in collectives:
collectives.remove(combo[0])
if combo[1] in collectives:
collectives.remove(combo[1])
collectives.extend(newcollectives)
collectives = list(set(collectives))
if set(collectives) == oldcollectives:
stop = True
for collective in collectives:
if first in collective and second in collective:
return True
return False
April 4, 2015