Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from copy import deepcopy
- import random
- # 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]
- c_copy = deepcopy(c)
- random.seed()
- 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)
- 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()
- c = [sorted(y) for y in c]
- def shuff(c, n):
- for i in range(n-1,0,-1):
- j = random.randint(0,i)
- c[i],c[j] = c[j],c[i]
- c.append(sorted(c[len(c)-1]))
- n = len(s)
- steps = n*(2**2)*((n*(2**2))-1)*((n*(2**2))-2)//6
- reset = 0
- c_elem = set()
- set_cover = []
- partial = []
- for a in range(0, steps + 1):
- reset = reset + 1
- if reset > len(c):
- c_elem = set()
- reset = 0
- shuff(c, len(c))
- c = sorted(c, key=lambda parameter: parameter[0])
- 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(k)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement