Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
This is the way solution in Clear category for How to Find Friends by Pavellver
def check_connection(network, first, second):
network = [sorted(pair.split('-')) for pair in network]
way = [first]
while any(network) and way:
n = len(way)
for pair in network:
for i in range(n):
if way[i] in pair:
way.append(pair[0] if way[i] == pair[1] else pair[1])
if second in way:
return True
for x in way[:n]:
for y in way[n:]:
if sorted([x, y]) in network:
network.remove(sorted([x, y]))
way = way[n:]
return False
May 25, 2023
Comments: