Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Sets manipulation with early exit solution in Clear category for How to Find Friends by igharok
def check_connection(network, first, second):
bool_connected = False
set_friends = {first}
set_ex_friends = set()
while(bool(len(set_friends)) & (not bool_connected)):
next_friend = set_friends.pop()
set_ex_friends.add(next_friend)
for iter_connect in network:
set_pair = set(iter_connect.split('-'))
if (next_friend in set_pair):
set_friends = set_friends.union(set_pair.difference(set_ex_friends))
if (second in set_friends):
bool_connected = True
return bool_connected
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."
Feb. 19, 2020
Comments: