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 saklar13
def check_connection(network, first, second):
l = []
for connect in network:
a, b = connect.split('-')
for row in l:
if a in row or b in row:
row.add(a)
row.add(b)
break
else:
l.append({a, b})
i = 0
while i < len(l) -1:
for comp in l[i]:
if comp in l[i+1]:
l[i].update(l[i+1])
del l[i+1]
break
else: i += 1
for row in l:
if first in row and second in row:
return True
else: 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