Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Broken rings solution in Uncategorized category for Break Rings by OlgaMardar
def break_rings(ring_tuple):
a = 0
b = []
mas_v = []
ring_count = {}
work_array = []
delete_ring = 0
count_del_ring = 0
remove_elem = []
remove_mas = []
for elem in ring_tuple:
b = list(elem)
if not (b[0] in mas_v):
mas_v.append(int(b[0]))
if not (b[1] in mas_v):
mas_v.append(int(b[1]))
work_array.append(b)
mas_v.sort()
for elem in mas_v:
count = 0
for el in ring_tuple:
if elem in el:
count = count + 1
ring_count[elem] = count
count_del_ring = 0
i = 0
while work_array!=[] and i<100:
i = i + 1
min = 100000
for elem in ring_count:
if ring_count[elem] < min and ring_count[elem]!=0:
min = ring_count[elem]
delete_ring = 0
nei_del = 0
for elem in ring_count:
if ring_count[elem] == min:
nei_del = int(elem)
find = 0
for elem in work_array:
if find == 0:
if elem[0] == nei_del:
delete_ring = int(elem[1])
find = 1
if elem[1] == nei_del:
delete_ring = int(elem[0])
find = 1
ring_count[delete_ring] = 0
count_del_ring = count_del_ring + 1
remove_elem = []
for elem in work_array:
if delete_ring in elem:
remove_elem.append(elem)
for elem in remove_elem:
work_array.remove(elem)
for elem in mas_v:
count = 0
for el in work_array:
if elem in el:
count = count + 1
ring_count[elem] = count
return count_del_ring
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 7, 2015
Comments: