2017-08-28 95 views
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) 
+0

有在这里没有任何的问题,但它听起来像是你要我们写代码你。试着去消除重言式。一旦你卡住了,发布[mcve]。 –

+0

我的问题是如果你有关于如何去除重言式的想法。在两条#评论(TAUTOLOGY CONTROL-HERE TAUTOLOGIES END)之间有我的代码,我尝试删除它们。所以在那部分代码中,我被卡住了。 –

+0

未来,如果你回答自己的问题,把它作为一个答案,而不是编辑你的问题。那些有类似担忧的人可以看到问题和解决方案。 –

回答

0

这就是答案

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)