2017-06-15 75 views
0

遠離python一段時間,因此格式化技能不存在。展望把這個格式的東西:格式化爲3SAT格式表

[[8, -6, -4], [-10, 4, 6], [6, -8, -9]] 

到的東西,看起來像這樣:

(x8 v ~x6 v ~x4)^(~x10 v x4 v x6)^(x6 v ~x8 v ~x9) 

,然後能夠引用的每個數字作爲單獨的輸入改爲T或F.任何指導意見會很高興。

import random 
def sample(): 
    nums = random.sample(range(-10, 10), 3) 
    return nums 

exlist = [] 
boundary = random.randint(3, 10) 
count = 0 
while (count < boundary): 
    count = count + 1 
    exlist.append(sample()) 

回答

1

注意0的情況下,似乎淪爲你的情況,所以我想你沒有0(否則它成爲X0或〜X0?)

假設你存儲T/F在名單如下:

exvalues = [bool(random.randint(0,1)) for _ in range(10)] 

,那麼你可以簡單地評價這樣的表達(而不需要生成符號表達你以後):

all(any(exvalues[x] if x>0 else not exvalues[-x] for x in y) for y in exlist) 

說明:您取所有「子表達式」的全局and,並且每個子表達式都是元素的全局or(可選not,具體取決於符號)。

+0

我不知道如何生成一個隨機列表與否定並排除零。 –