Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for How to Find Friends by vikulin
def check_connection(network, first, second):
sets = []
for pair in network:
new_set = pair = set(pair.split('-'))
for t in sets[:]: # must use a copy to be able to remove items
if t & pair: # pair is in the current set
new_set |= t # unite new_set with the current set
sets.remove(t) # remove current set from sets
sets.append(new_set)
return any({first, second} <= set for set in sets)
Sept. 27, 2014