Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First graphs expirience solution in Clear category for How to Find Friends by Kolia951
import collections
def preparing_data(network):
neighbours = collections.defaultdict(list)
points = []
for pair in network:
first, second = pair.split("-")
neighbours[first].append(second)
neighbours[second].append(first)
points = [key for key in neighbours.keys()]
visited = dict.fromkeys(points, 0)
return neighbours, points, visited
def check_connection(network, first, second):
neighbours, points, visited = preparing_data(network)
stack = []
stack.append(first)
while stack:
elem = stack.pop(0)
visited[elem] = 1
for neighbour in neighbours[elem]:
if not visited[neighbour] and neighbour not in stack:
stack.append(neighbour)
else:
continue
if second in stack:
return True
else:
continue
return False
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"scout2", "scout3") == True, "Scout Brotherhood"
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"super", "scout2") == True, "Super Scout"
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"dr101", "sscout") == False, "I don't know any scouts."
Jan. 20, 2023
Comments: