Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Sets of Friends solution in Clear category for How to Find Friends by adamspj
def build_set(network: list[list[str]], individual: str) -> set[str]:
related = set()
related.add(individual)
checked = []
while True:
size = len(related)
new_names = []
for item in related:
if not item in checked: # Speed it up a bit by not rechecking the same items over and over
new_names.extend([name for relationship in network if item in relationship for name in relationship])
checked.append(item)
related.update(new_names)
if len(related) == size: # The set has not grown in this iteration, so we're done
break
return related
def check_connection(network, first, second):
network = [connection.split('-') for connection in network]
return not build_set(network, first).isdisjoint(build_set(network, second))
May 11, 2024
Comments: