Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Dictionary Method solution in Clear category for How to Find Friends by daustoe
def check_connection(network, first, second):
networks = {}
for connection in network:
p1, p2 = connection.split('-')
networks.setdefault(p1, []).append(p2)
networks.setdefault(p2, []).append(p1)
queue = [first]
travelled = []
while len(queue):
current = queue.pop()
travelled.append(current)
if current == second:
return True
for next in networks.setdefault(current, []):
if next not in travelled and next not in queue:
queue.append(next)
return False
Aug. 15, 2017