Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Group nodes in classes solution in Uncategorized category for New Cities by Rounin
def subnetworks(net, crushes):
neighbours = dict()
crushed = set()
nodes = set()
for pair in net:
if not pair[0] in neighbours:
neighbours[pair[0]] = list()
if not pair[1] in neighbours:
neighbours[pair[1]] = list()
neighbours[pair[0]].append(pair[1])
neighbours[pair[1]].append(pair[0])
nodes.add(pair[0])
nodes.add(pair[1])
for crush in crushes:
crushed.add(crush)
visited = set()
classes = {}
nextnodes = []
for node in nodes:
if not node in crushed and not node in visited:
nextnodes.append(node)
visited.add(node)
while len(nextnodes) > 0:
currentnode = nextnodes.pop()
visited.add(currentnode)
if currentnode not in classes:
classes[currentnode] = currentnode
for neighbour in neighbours[currentnode]:
if not neighbour in visited and not neighbour in crushes:
classes[neighbour] = classes[currentnode]
nextnodes.append(neighbour)
return len(set(classes.values()))
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!')
July 21, 2017