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 jcg
#find-friends
def check_connection(network, first, second):
partition = [] # list of dijoint sets of connected drones
for relation in network :
newset = set(relation.split('-')) # a new set of connected drones
for s in partition[:] :
if newset.intersection(s) : # if common drone s and newset
newset.update(s) # merge s with newset
partition.remove(s) # s is no longer member of partition
partition.append(newset) # newset with merged sets wihch are member of partition
return any(map(lambda s : set((first, second))<=s, partition))
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."
June 4, 2014