Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for New Cities by mortonfox
def subnetworks(net, crushes):
nodes = set(sum(net, [])) - set(crushes)
subcount = 0
while nodes:
subcount += 1
stack = [nodes.pop()]
while stack:
node = stack.pop()
for conn in net:
if node == conn[0] and conn[1] in nodes:
stack.append(conn[1])
nodes.remove(conn[1])
if node == conn[1] and conn[0] in nodes:
stack.append(conn[0])
nodes.remove(conn[0])
return subcount
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!')
May 18, 2017