Close

Tidying up my Rough Drafts

A project log for Attempt @ BPP algorithim for an NP-hard problem

Exact Three Cover.

Tea BTea B 09/02/2020 at 18:580 Comments
import random
from itertools import permutations
import json

# |S| creation 52
s = []
for a in range(1, 52):
    s.append(a)

# Contains only One Solution

c = [[4, 5, 39], [10, 11, 17], [4, 5, 44], [1, 2, 27], [1, 2, 41], [10, 11, 31], [4, 5, 51], [10, 11, 40], [1, 2, 37], [10, 11, 29], [7, 8, 24], [4, 5, 7], [4, 5, 35], [7, 8, 51], [1, 2, 44], [7, 8, 38], [7, 8, 40], [1, 2, 17], [7, 8, 9], [7, 8, 17], [4, 5, 11], [7, 8, 31], [1, 2, 3], [4, 5, 36], [10, 11, 23], [4, 5, 31], [10, 11, 51], [10, 11, 36], [1, 2, 48], [7, 8, 42], [16, 17, 18], [10, 11, 21], [1, 2, 7], [7, 8, 48], [10, 11, 42], [7, 8, 21], [7, 8, 10], [4, 5, 24], [34, 35, 36], [1, 2, 25], [1, 2, 46], [7, 8, 26], [1, 2, 50], [37, 38, 39], [1, 2, 21], [46, 47, 48], [4, 5, 13], [1, 2, 39], [1, 2, 19], [7, 8, 35], [4, 5, 8], [7, 8, 47], [1, 2, 22], [25, 26, 27], [7, 8, 22], [4, 5, 20], [4, 5, 9], [4, 5, 22], [10, 11, 46], [4, 5, 23], [7, 8, 41], [10, 11, 38], [1, 2, 31], [7, 8, 12], [1, 2, 49], [10, 11, 48], [10, 11, 45], [10, 11, 18], [4, 5, 16], [4, 5, 18], [28, 29, 30], [4, 5, 33], [1, 2, 20], [1, 2, 24], [7, 8, 32], [4, 5, 27], [7, 8, 36], [4, 5, 28], [4, 5, 29], [7, 8, 18], [1, 2, 42], [1, 2, 32], [1, 2, 9], [1, 2, 23], [1, 2, 14], [1, 2, 36], [4, 5, 32], [4, 5, 45], [7, 8, 20], [7, 8, 34], [4, 5, 37], [1, 2, 38], [31, 32, 33], [7, 8, 43], [4, 5, 21], [10, 11, 44], [1, 2, 6], [7, 8, 25], [4, 5, 50], [10, 11, 22], [7, 8, 50], [10, 11, 27], [7, 8, 28], [10, 11, 26], [1, 2, 47], [4, 5, 38], [4, 5, 30], [1, 2, 12], [10, 11, 50], [10, 11, 13], [10, 11, 19], [1, 2, 33], [4, 5, 19], [4, 5, 46], [7, 8, 46], [1, 2, 30], [7, 8, 27], [10, 11, 34], [10, 11, 39], [1, 2, 26], [4, 5, 17], [4, 5, 41], [4, 5, 10], [10, 11, 49], [10, 11, 32], [1, 2, 43], [19, 20, 21], [10, 11, 15], [7, 8, 15], [1, 2, 35], [1, 2, 8], [7, 8, 37], [7, 8, 44], [7, 8, 13], [22, 23, 24], [10, 11, 47], [10, 11, 24], [7, 8, 30], [10, 11, 12], [10, 11, 35], [7, 8, 49], [4, 5, 48], [1, 2, 4], [1, 2, 28], [10, 11, 28], [1, 2, 13], [1, 2, 16], [10, 11, 20], [7, 8, 14], [1, 2, 10], [40, 41, 42], [1, 2, 51], [1, 2, 34], [10, 11, 16], [7, 8, 11], [10, 11, 33], [7, 8, 23], [7, 8, 45], [43, 44, 45], [1, 2, 45], [1, 2, 18], [4, 5, 25], [4, 5, 47], [13, 14, 15], [4, 5, 14], [4, 5, 40], [4, 5, 34], [7, 8, 33], [1, 2, 15], [10, 11, 43], [4, 5, 49], [1, 2, 5], [10, 11, 37], [7, 8, 19], [49, 50, 51], [1, 2, 11], [4, 5, 43], [7, 8, 16], [4, 5, 6], [1, 2, 29], [4, 5, 26], [7, 8, 39], [4, 5, 12], [7, 8, 29], [1, 2, 40], [4, 5, 15], [4, 5, 42], [10, 11, 25], [10, 11, 14], [10, 11, 30], [10, 11, 41]]

#c =[[1,2,3],[1,2,4],[1,2,5],[1,2,6],[3,4,5],[3,4,6],[3,4,7],[4,5,6],[4,5,7],[6,3,7],[7,10,11],[22,1,11],[22,27,29],[30,1,14],[30,13,14],[14,20,11],[25,27,29],[1,2,45],[14,1,2],[14,2,3],[14,5,1],[26,4,13],[26,7,17],[27,29,18],[19,27,13],[3,4,13],[19,2,1],[19,20,9],[18,3,2],[29,26,1],[5,3,11],[13,16,19],[13,20,21],[13,1,9],[17,19,23],[1,3,5],[5,3,2],[6,8,1],[7,30,23],[11,19,7],[7,4,28],[7,4,28],[18,13,1],[7,12,15],[15,2,3],[29,1,2],[29,2,3],[29,2,4],[29,5,4],[29,23,22],[3,26,5],[3,26,4],[3,25,3],[10,11,2],[30,1,2],[30,3,4],[30,4,5],[30,8,9],[30,18,19],[30,20,13],[6,22,23],[6,24,15],[18,1,2],[18,2,3],[18,4,5],[18,16,1],[24,26,27],[10, 2, 7], [10, 13, 14], [10, 5, 9], [6, 3, 8], [10, 26, 27], [10, 19, 16], [10, 25, 23], [10, 12, 15], [10, 29, 30], [10, 20, 21],[27,28,30],[28,29,30],[10,2,1],[10,7,8],[10,13,14],[10,11,8],[15,1,2],[15,8,4],[13,14,15],[11,12,15],[22,1,2],[22,21,3],[22,25,23],[10,4,8],[17,2,3],[17,2,4],[17,20,21],[18,2,3],[18,2,4],[18,19,16],[15,4,9],[4,5,1],[4,5,10],[5,7,9],[4,5,9]]

# Solve Exact List Cover.

# Just unhashtag these to enter inputs manually.
#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]

n = len(s)
while_loop_steps = n*241*((n*241)-1)*((n*241)-2)//6

if len(s) % 3 != 0:
    print('invalid input')
    quit()

# Sort list to remove
# lists like [1,2,3] and [1,3,2]
# but leave [1,2,3].
# If I wanted too use Brute force
# just needs one. Shortens list
# and improves execution time.

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 lists
# 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 lists
# 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 lists

c = [c[x] for x in range(len(c)) if not(c[x] in c[:x])]

# sort list from smallest to largest
new_c = []
for a in range(0, len(c)):
    new_c.append(sorted(tuple(c[a])))

# sort c into like lists
# (eg. [1,2,x], with all [1,2,x]'s

sorted_new_c = []
for a in range(0, len(new_c)):
    for b in new_c[a+1:]:
        if c[a][0] == b[0]:
            if c[a][1] == b[1]:
                sorted_new_c.append(b)
                sorted_new_c.append(c[a])

# remove repeating lists
sorted_new_c = [sorted_new_c[x] for x in range(len(sorted_new_c)) if not(sorted_new_c[x] in sorted_new_c[:x])]

# I don't want to forget the other lists
surpise_second_while_loop = []
for a in new_c:
    if a not in sorted_new_c:
        surpise_second_while_loop.append(a)
        sorted_new_c.append(a)
        
c = sorted_new_c
stop = 0
Potential_solution = []
opps = 0
failed_lists = 0
ss = s

#def true_random():
# I am working on using a USB laser
# so that I have my own True Random
# Number Generator
    
k = 870,782,524,200,987,829,741,339,528,214,217,490,25,805,249,156,263,476,848,873,487,985,741,743,301,1,855,134,957,264,649,274,713,348,738,819,740,95,290,324,255,425,912,69,718,602,759,681,124,673,919,378,249,482,724,989,823,466,155,637,526,883,769,696,51,917,501,748,405,374,226,109,205,946,177,16,899,622,973,773,909,959,673,865,736,730,182,664,225,982,413,397,463,224,644,283,870,782,776,476,256,664,755,718,70,263,38,437,996,7,369,981,72,14,836,525,654,975,48,203,617,773,101,863,992,221,374,567,886,492,743,516,725,911,747,767,908,145,794,440,235,447,733,531,532,649,875,908,318,175,160,31,236,978,395,177,452,663,945,271,654,336,497,430,526,290,824,379,692,65,960,764,68,498,73,33,606,54,363,641,931,171,553,29,60,341,497,358,504,385,35,740,595,556,26,641,86,425,397,908,164,203,783,302,817,746,913,682,468,583,155,311,339,333,625,189,448,239,157,257,189,546,618,295,400,977,605,192,472,394,692,915,913,249,128,820,610,956,33,712,549,436,999,933,933,864,46,866,984,563,76,150,260,512,441,3,534,516,520,885,2,305,965,71,740,57,891,390,148,972,689,719,294,725,757,871,808,941,187,202,274,44,346,104,948,572,19,381,886,676,265,479,941,709,975,603,148,892,816,577,132,605,537,614,138,227,641,188,37,750,173,278,502,323,946,771,848,998,652,865,324,261,732,819,362,910,60,504,273,12,508,760,566,597,991,99,759,526,981,942,294,45,247,321,120,641,759,505,631,396,187,910,944,357,618,800,642,36,245,113,641,333,460,517,411,783,563,501,21,25,546,671,729,73,552,831,274,868,849,986,234,427,467,509,302,294,725,902,451,444,455,23,797,975,590,575,703,909,939,729,972,978,190,133,239,254,416,507,543,704,95,300,76,462,192,14,529,342,230,116,785,393,844,45,692,440,84,231,577,643,107,33,973,187,671,990,79,122,778,627,441,125,193,673,570,18,482,272,746,535,47,873,773,613,356,987,968,924,389,651,622,202,676,854,82,566,535,590,972,876,742,615,646,514,688,618,209,302,292,210,488,387,63,490,558,800,741,930,841,274,371,706,774,611,731,142,793,219,789,94,678,584,545,503,730,587,14,270,303,739,681,47,352,727,158,673,890,621,52,359,437,906,396,785,727,94,939,294,733,411,961,398,633,331,840,380,858,989,683,162,958,20,952,351,749,514,460,462,895,488,205,441,611,396,867,347,141,267,379,151,855,780,77,703,285,159,48,259,71,349,965,289,586,488,331,83,435,679,711,155,997,583,92,130,568,453,857,67,796,729,891,538,748,666,726,800,643,468,25,505,446,34,62,677,97,773,914,676,3,124,578,218,337,628,602,682,52,239,181,912,615,329,870,505,992,367,675,43,115,421,13,983,145,891,925,363,142,208,214,704,445,143,449,323,266,800,860,484,458,570,287,254,606,947,285,36,317,965,769,126,716,973,856,535,481,917,956,743,71,802,427,542,791,362,993,758,770,181,817,502,18,97,586,514,621,687,472,164,184,771,981,5,694,179,581,601,620,578,955,261,589,349,847,729,647,944,923,933,191,847,739,625,884,208,2,875,28,809,264,901,444,267,652,526,225,372,323,821,499,829,757,949,893,186,938,907,65,195,909,412,167,679,808,834,912,258,665,18,748,678,193,634,15,530,677,579,562,565,930,992,396,133,353,382,186,445,881,830,231,624,565,915,556,651,641,638,542,303,758,1000,859,668,452,332,522,992,450,605,908,828,602,422,297,361,598,411,717,142,226,596,96,106,590,746,498,128,490,558,273,975,215,445,396,694,692,901,148,108,312,123,257,965,998,643,961,893,17,555,202,193,111,501,345,847,769,477,924,363,870,891,346,813,816,31,121,659,733,491,631,792,602,74,941,984,350,972,162,572,153,666,953,376,212,732,222,449,296,322,731,146,247,853,902,12,519,944,453,957,595,731,593,635,733,849,658,664,939,386,118,469,671,340,774,332,594,882,426,605,554,778,966,387,505,267,452,52,585,561,645,64,25,771,926,877,715,603,59,552,42,644,423,660,891,664,94,666,775,84,79,550,663,949,602,456,83,231,79,261,455,7,386,349,645,889,256,617,889,555,632,192,954,403,35,972,889,365,861,524,537,88,928,604,128,

        
    

Discussions