Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for New Cities by tom-tom
def subnetworks(net, crushes):
links = [{x, y} for x, y in net if x not in crushes and y not in crushes]
nodes = set().union(*net).difference(crushes)
result = 0
while nodes:
result += 1
segment = {nodes.pop()}
segment_n = 0
while segment_n < len(segment):
segment_n = len(segment)
for link in links:
if link & segment:
segment.update(link)
nodes -= segment
return result
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!')
Aug. 24, 2017