1
我有一個AI練習要做。目標是在屏幕上打印CNF(n,m,k)。它是一個帶有n個字母,m個子句的布爾表達式,每個子句必須有k個字母。我有這個規則,大寫字母是真的,小寫字母是假的。此外,我必須從條款中刪除冗餘和重言式。如CNF(6,3,3)可能是這樣的:CNF與Python,如何去除重言式
[[A,true]or[b,false]or[D,true]] and
[[B,true]or[d,false]or[a,false]] and
[[d,false]or[A,true]or[B,true]]
,你可以在這裏看到我們有3項條款正好與3個字母和字母是6個字母(6資本和6小)。這裏我們沒有任何冗餘或重言式。冗餘就像[[A,true]或[A,true]]。重言式就像[[A,true]或[A,false]]。 現在我發佈我的Python代碼,它的工作原理,但我只刪除冗餘。所以,正如我在這個問題中寫的那樣,我需要刪除重言式。 下面的代碼
import random
import string
import sys
import os
#This function create the list of tuple
# n = number of symbols, m = nuber of clauses, k = clause length
def createTuple(n):
if n > 26:
print("it's impossible to have more than 25 letters")
sys.exit()
# creo lista di 25 numeri e la mescolo
my_list = [0] * (2 * n)
for x in range(0, 2 * n):
my_list[x] = x
random.shuffle(my_list)
# creo la lista di simboli minuscoli o maiuscoli
symbols = [0] * (2 * n)
x = 0
for count in range(0, n):
symbols[count] = string.ascii_lowercase[count]
for count in range(n, 2 * n):
symbols[count] = string.ascii_uppercase[count - n]
symbols_rand = [0] * (2 * n)
for count in range(0, 2 * n):
symbols_rand[count] = symbols[my_list[count]]
list_tuple = [0] * (2 * n)
for counts in range(0, (2 * n)):
if symbols_rand[counts].islower():
list_tuple[counts] = [(symbols_rand[counts]), False]
if symbols_rand[counts].isupper():
list_tuple[counts] = [(symbols_rand[counts]), True]
return list_tuple
#This function create the CNF
def createCnf(n, m, k):
list_tuple = createTuple(n)
stot = ""
ki = ""
x = 0
i = 0
countx = 0
for count in range(0, m):
ktot = "{"
temple_tuple = createTuple(n)
temple_tuple2 = []
for index in range(0, k):
#REDUNDANCY CONTROL
i = index
while(temple_tuple[i] == None):
i += 1
countx = 0
#TAUTOLOGY CONTROL (WORKING ONLY WHEN THE TWO ELEMENTS ARE CLOSE)
while countx < len(temple_tuple2):
x = temple_tuple[i][0]
if temple_tuple2[countx][0].isupper():
c = temple_tuple2[countx][0].lower()
else:
c = temple_tuple2[countx][0].upper()
if (x == c):
i += 1
print("tautologia trovata")
countx = 0
else:
countx = countx + 1
ki = temple_tuple[i]
temple_tuple2.append(ki)
temple_tuple[i] = None
if index != (k - 1):
ktot += str(ki) + " or "
elif index == (k - 1):
ktot += str(ki)
# HERE TAUTOLOGIES END
ktot += "}"
if count != (m - 1):
stot += ktot + " and "
elif count == (m - 1):
stot += ktot
print(stot)
lista_tuple = createTuple(6)
createCnf(6,3,3)
有在這裏沒有任何的問題,但它聽起來像是你要我們寫代碼你。試着去消除重言式。一旦你卡住了,發佈[mcve]。 –
我的問題是如果你有關於如何去除重言式的想法。在兩條#評論(TAUTOLOGY CONTROL-HERE TAUTOLOGIES END)之間有我的代碼,我嘗試刪除它們。所以在那部分代碼中,我被卡住了。 –
未來,如果你回答自己的問題,把它作爲一個答案,而不是編輯你的問題。那些有類似擔憂的人可以看到問題和解決方案。 –