Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for How to Find Friends by UFO665
def unionSets(lstSets):
for i, s1 in enumerate(lstSets):
for ii in range(i + 1, len(lstSets)):
if lstSets[i].intersection(lstSets[ii]):
newSet = lstSets[i].union(lstSets[ii])
lstSets.append(newSet)
lstSets.pop(ii)
lstSets.pop(i)
return lstSets
return lstSets
def createSets(network):
lstSets = []
for pair in network:
name1, name2 = pair.split("-")
bFound = False
for s in lstSets:
if name1 in s and name2 not in s:
s.add(name2)
break
elif name2 in s and name1 not in s:
s.add(name1)
break
else:
lstSets.append({name1, name2})
lstSets = unionSets(lstSets)
return lstSets
def check_connection(network, first, second):
return any(all(name in s for name in [first, second]) for s in createSets(network))
Dec. 8, 2015