Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import groupby
- import random
- from itertools import combinations
- from itertools import permutations
- s = 1,2,3,4,5,6,7,8,9
- c = [[1,2,3],[4,5,7],[4,5,6],[1,2,8],[7,8,9],[1,2,4],[1,2,5],[1,2,7],[1,4,5]]
- # I am creating a value
- # so that I can break in
- # an upcoming while loop
- xx = s * 2
- combinations_count = 0
- for i in combinations(xx, 3):
- combinations_count = combinations_count + 1
- if len(s) % 3 != 0:
- print('invalid input')
- quit()
- ss = s
- # Sort list to remove
- # sets like (1,2,3) and (1,3,2)
- # but leave (1,2,3)
- delete = []
- for a in range(0, len(c)):
- for i in permutations(c[a], 3):
- if list(i) in c[a:]:
- if list(i) != c[a]:
- delete.append(list(i))
- for a in range(0, len(delete)):
- if delete[a] in c:
- del c[c.index(delete[a])]
- # remove sets
- # that have
- # repeating
- # elements
- remove = []
- for rem in range(0, len(c)):
- if len(c[rem]) != len(set(c[rem])):
- remove.append(c[rem])
- for rem_two in range(0, len(remove)):
- if remove[rem_two] in c:
- del c[c.index(remove[rem_two])]
- # remove sets
- # that have
- # elements
- # that don't
- # exist in S.
- remove=[]
- for j in range(0, len(c)):
- for jj in range(0, len(c[j])):
- if any(elem not in s for elem in c[j]):
- remove.append(c[j])
- for rem_two in range(0, len(remove)):
- if remove[rem_two] in c:
- del c[c.index(remove[rem_two])]
- # Remove repeating sets
- solutions =[c[x] for x in range(len(c)) if not(c[x] in c[:x])]
- c = solutions
- length = len(solutions)
- ss = s
- # while loop to see
- # if we can find
- # an Exact Three Cover
- # in poly-time.
- stop = 0
- w = 0
- new = []
- # Shuffle these
- # annoying
- # counter-examples
- # that might ruin
- # my day
- print('Initalizing', combinations_count,'shuffles')
- for a in range(0, combinations_count):
- random.shuffle(c)
- stop = 0
- while True:
- # Shuffling c randomly
- # to reduce chance of
- # error.
- random.shuffle(c)
- if len(new) == len(ss) // 3:
- # break if Exact
- # three cover is
- # found.
- #print(new)
- w = w + 1
- print(new, 'yes')
- print('took',stop,'steps in while loop')
- break
- # Keep c[0]
- # and append to
- # end of list
- # del c[0]
- # to push >>
- # in list.
- c.append(c[0])
- del [c[0]]
- new = []
- s = set()
- stop = stop + 1
- for l in c:
- if not any(v in s for v in l):
- new.append(l)
- s.update(l)
- if len(new) != len(ss)//3:
- if stop == length:
- # Reverse list just
- # to reduce false -/+
- # answer.
- for cc in range(0, len(c)):
- c = list(reversed(c))
- random.shuffle(c)
- if stop >= combinations_count:
- print('no')
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement