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 makoto_yamagata
def check_connection(network, first, second):
def finder(network, start, goal):
if goal in start: return True
if not start : return False
friends = { node for node in network if start.intersection(node) }
foaf_names = [ list(node.difference(start)) for node in friends ]
next_start = { x[0] for x in foaf_names if x }
next_network = network.difference(friends)
return finder(next_network, next_start, goal)
return finder({ frozenset(x.split('-')) for x in network }, {first}, second)
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."
July 11, 2015