Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for How to Find Friends by yukirin
def check_connection(network, first, second):
ns = []
for a, b in map(lambda n: n.split('-'), network):
na = next(n for n in ns + [set((a,))] if a in n)
nb = next(n for n in ns + [set((b,))] if b in n)
if na in ns: ns.pop(ns.index(na))
if nb in ns: ns.pop(ns.index(nb))
ns.append(na.union(nb))
return bool([True for n in ns if n >= set((first, second))])
if __name__ == '__main__':
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"scout2", "scout3") is True, "Scout Brotherhood"
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"super", "scout2") is True, "Super Scout"
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"dr101", "sscout") is False, "I don't know any scouts."
April 6, 2015