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 PsnDth
def check_connection(network, first, second):
connections = []
members = []
rel = {}
for x in network:
connections.extend([x.split('-')])
for x in connections:
for y in x:
if y not in members:
members.append(y)
l = len(members)
for x in members:
rel[x] = []
for y in connections:
if x in y:
if y.index(x) == 1:
rel[x].append(y[0])
else:
rel[x].append(y[1])
while l > 0:
if first in rel[second]:
return True
break
else:
for x in rel[second]:
for y in rel[x]:
if y not in rel[second]:
rel[second].append(y)
l = l- 1
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."
Dec. 26, 2014