Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Break Rings by MichalMarsalek
import itertools
def break_rings(rings):
cons = rings
rings = set()
for con in cons:
rings.add(tuple(con)[0])
rings.add(tuple(con)[1])
for i in range(1, len(rings)+1): #Iterate trought number of ring broken
for broken_rings in itertools.combinations(rings, i): #Iterate trought all combinations of broken rings
ok = True
for ring in rings: #Iterate trought all rings
if ring in broken_rings:
continue #Check next ring as this one was removed
for con in cons: #Iterate trought all connections
if ring not in con:
continue #Check next connection as the ring is not in this one
other_ring = tuple(con.difference({ring}))[0] #Get the other ring in the connection
if other_ring not in broken_rings:
ok = False #Go on and try to brake more rings as this one is still connected to another one
break
if not ok:
break #Go on and try to brake more rings as this one is still connected to another one
if ok:
return i #If it's ok, return it
Aug. 16, 2015
Comments: