Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Break Rings by Kurush
import itertools
def get_numbers_from_rings(rings):
numbers = set()
for ring in rings:
numbers = numbers.union(ring)
return numbers
def is_break_combination(rings, combination):
for ring in rings:
if not (set(ring) & set(combination)):
return False
return True
def break_rings(rings):
numbers = get_numbers_from_rings(rings)
for i in range(1, len(numbers) + 1):
for combination in itertools.combinations(numbers, i):
if is_break_combination(rings, combination): return i
return -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,2],[1,6],[6,7],[7,8],[8,9],[9,6],[1,10],[10,11],[11,12],[12,13],[13,10],[1,14],[14,15],[15,16],[16,17],[17,14]]) == 8, 'Long'
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"
May 28, 2021