Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Straightforward, not very pythonic solution in Clear category for New Cities by martin_b
def subnetworks(net, crushes):
domains, crushes = [], set(crushes)
for c1, c2 in net:
d1 = None
if not c1 in crushes:
for d1 in domains:
if c1 in d1:
break
else:
d1 = {c1}
domains.append(d1)
if not c2 in crushes:
for i, d2 in enumerate(domains):
if c2 in d2:
if d1 != None:
del domains[i]
d1 |= d2
break
else:
if d1 == None:
d1 = set()
domains.append(d1)
d1.add(c2)
return len(domains)
June 21, 2019