Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
four for solution in Clear category for Break Rings by freeman_lex
from collections import Counter as c
from itertools import combinations as com
def break_rings(r):
M = dict(c(j for i in r for j in i))
S = set(M)
for x in range(1, len(S)):
for y in com(S, x):
M1 = M.copy()
for z in y:
del(M1[z])
for j in r:
if z in j and sum(j) - z in M1:
M1[sum(j) - z] -= 1
if all(not j for j in M1.values()):
return len(M) - len(M1)
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"
April 5, 2016
Comments: