Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from copy import deepcopy
- import random
- import quantumrandom
- # Seeding the PRNG with True Randomness
- # from a Quantum Random Number Generator
- seed = quantumrandom.randint(1, 100)
- random.seed(int(seed))
- # integers only and list of lists only
- s = input("Input list of integers (no repeating elements) for S with commas (eg. 1,2,3,4...) : ").split(',')
- c = input('enter list for C (eg. [[1,2,3],[4,5,6]]): ')
- c = json.loads(c)
- s = [int(a) for a in s]
- miss = []
- delete = []
- def input_validation():
- # checking for missing elements.
- for a in c:
- for b in a:
- miss.append(b)
- for d in s:
- if d not in miss:
- print('False', d)
- # Throw out unwanted orderings of sub-lists
- for a in range(0, len(c)):
- c[a] = sorted(c[a])
- # Lists are treated as sets, so lists
- # with repeating elements is thrown out
- for r in range(0, len(c)):
- if len(c[r]) != len(set(c[r])):
- c[r] = [['xx']]
- # Throw out lists that have elements that do
- # not exist in s.
- # Throw out lists with more than 3
- # Throw out lists less than 3
- for a in range(0, len(c)):
- for b in c[a]:
- if c[a].count(b) > 1:
- delete.append(c[a])
- break
- if len(c[a]) != 3:
- delete.append(c[a])
- for bb in c[a]:
- if bb not in s:
- delete.append(c[a])
- for b in delete:
- if b in c:
- del c[c.index(b)]
- s_copy = deepcopy(s)
- c_copy = deepcopy(c)
- input_validation()
- # remove repeating lists
- c = [c[x] for x in range(len(c)) if not(c[x] in c[:x])]
- def bijective_mapp():
- for a in range(0, len(s)):
- s[a] = a+1
- for b in range(0, len(c)):
- for bb in range(0, len(c[b])):
- c[b][bb] = s_copy.index(c[b][bb])+1
- bijective_mapp()
- # shuffle the groups
- # of sorted c[m][0]'s
- # without losing the
- # sort
- def shuffle_subgroups():
- sublist = []
- new_c = []
- count = 0
- X = []
- while count != max(cc):
- count = count + 1
- for a in range(0, len(cc)):
- if cc[a] == count:
- sublist.append(c[a])
- if a == len(cc)-1:
- new_c.append(list(sublist))
- sublist = []
- random.shuffle(new_c)
- for a in new_c:
- for b in a:
- X.append(b)
- return X
- n = len(s)
- steps = n*(2**1)*((n*(2**1))-1)*((n*(2**1))-2)//6
- reset = 0
- c_elem = set()
- set_cover = []
- partial = []
- bookmark = 0
- for a in range(0, steps + 1):
- reset = reset + 1
- if reset > len(c):
- random.shuffle(c)
- c_elem = set()
- reset = 0
- c = sorted(c, key=lambda parameter: parameter[0])
- if bookmark == 0:
- cc = [a[0] for a in c]
- bookmark = 1
- c = shuffle_subgroups()
- set_cover = []
- c.append(c[0])
- del(c[0])
- for l in c:
- if not any(v in c_elem for v in l):
- set_cover.append(l)
- c_elem.update(l)
- # if exact solution not found,
- # then use partial solution
- if set_cover not in partial:
- partial.append(set_cover)
- list_of_partials = [len(r) for r in partial]
- k = partial[list_of_partials.index(max(list_of_partials))]
- # Reversing the Bijective mapping
- # to the original input.
- for a in range(0, len(k)):
- for b in range(0, len(k[a])):
- l = s.index(k[a][b])
- k[a][b] = s_copy[l]
- for a in range(0, len(k)):
- k[a] = sorted(k[a])
- for a in c_copy:
- if sorted(a) in k:
- l = k.index(sorted(a))
- k[l] = a
- print("{:.0%}".format(len(k)/(len(s)//3)),'of the Universe is covered without overlap.',k)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement