Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Uncategorized category for New Cities by vvm70
def subnetworks(net, crushes):
nodes = list(filter(len, ([x for x in node if x not in crushes] for node in net)))
s = []
while nodes:
s.append(set(nodes.pop()))
for node in nodes:
if s[-1] & set(node):
s[-1] |= set(node)
nodes = [x for x in nodes if not (set(x) & s[-1])]
return len(s)
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert subnetworks([
['A', 'B'],
['B', 'C'],
['C', 'D']
], ['B']) == 2, "First"
assert subnetworks([
['A', 'B'],
['A', 'C'],
['A', 'D'],
['D', 'F']
], ['A']) == 3, "Second"
assert subnetworks([
['A', 'B'],
['B', 'C'],
['C', 'D']
], ['C', 'D']) == 1, "Third"
print('Done! Check button is waiting for you!')
Oct. 24, 2020