Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Fast breaker. Long code solution in Clear category for Break Rings by ermichin158
def break_rings(rings):
rings = list(rings)
for i in range(len(rings)):
rings[i] = list(rings[i])
allRings = getAllRings(rings)
return br(len(allRings), allRings, rings, 0)
def inArray(val, array):
return len([x for x in array if x == val]) != 0
def br(initLength, rings, connections, total):
if rings == []:
return 0
if connections == []:
return initLength - len(rings)
minCount = -1
minRing = -1
for i in rings:
tmc = getRingConnectionsCount(i, connections)
if (tmc < minCount or minCount == -1) and tmc > 0:
minCount = tmc
minRing = i
prl = getRingConnections(minRing, connections)
ringsToRemove = []
for connect in prl:
for r in connect:
if not inArray(r, ringsToRemove) and r != minRing:
ringsToRemove.append(r)
for r in ringsToRemove:
rings.remove(r)
removeConnections(r, connections)
return br(initLength, rings, connections, total + 1)
def removeConnections(ring, connections):
cons = getRingConnections(ring, connections)
for con in cons:
connections.remove(con)
def getRingConnectionsCount(ring, connections):
result = 0;
for rg in connections:
if rg[0] == ring or rg[1] == ring:
result += 1
return result
def getRingConnections(ring, connections):
result = [];
for rg in connections:
if rg[0] == ring or rg[1] == ring:
result.append(rg)
return result
def getAllRings(connections):
maximum = 0
for element in connections:
element = element
maximum = element[0] if element[0] > maximum and element[1] <= maximum else (element[1] if element[1] > maximum else maximum)
return list(range(1,maximum+1))
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert break_rings(({1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {4, 6})) == 3, "example"
assert break_rings(({1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4})) == 3, "All to all"
assert break_rings(({5, 6}, {4, 5}, {3, 4}, {3, 2}, {2, 1}, {1, 6})) == 3, "Chain"
assert break_rings(({8, 9}, {1, 9}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {8, 7})) == 5, "Long chain"
Nov. 12, 2015
Comments: