-5

I'm told to condense this in python in a more simplified form. Is it possible to do so in an orderly manner?

(not ((b or not c) and (not a or not c))) or (not (c or not (b and c))) or (a and not c) and (not a or (a and b and c) or (a and ((b and not c) or (not b))))

(not (a and not b) or (not c and b)) and (not b) or (not a and b and not c) or (a and not b))
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • 4
    https://en.wikipedia.org/wiki/Karnaugh_map – user2357112 Sep 20 '18 at 02:41
  • @user2357112 Karnaugh map is good to have bur isn't the "source" too complex to fill in the cells? – iBug Sep 20 '18 at 02:45
  • 1
    Not really sure what this question has to do with Python. The same procedure would need to be done in any language with boolean operators. Also https://en.wikipedia.org/wiki/Truth_table – OneCricketeer Sep 20 '18 at 02:55

1 Answers1

1

You can use sympy to evaluate and simplify boolean expressions:

(Note: the parser in this example is rather naive):

from sympy.logic.boolalg import to_dnf
from sympy.abc import a, b, c

def translate(expr):
    e = list(expr)
    res = [' ' for _ in range(len(e))]
    for start in range(len(e)):
        if expr[start: start+3] == 'not':
            res[start] = '~'
            res[start+1] = ''
            res[start+2] = ''
        elif expr[start: start+3] == 'and':
            res[start] = '&'
            res[start+1] = ''
            res[start+2] = ''
        else:
            if res[start] == ' ':
                res[start] = e[start]

    expr = ''.join(res)         
    e = list(expr)
    res = [' ' for _ in range(len(e))]
    for start in range(len(e)):
        if expr[start: start+2] == 'or':
            res[start] = '|'
            res[start+1] = ''
        else:
            if res[start] == ' ':
                res[start] = e[start]

    res = [elt for elt in res if elt != ' ' or elt != '']
    return ''.join(res)


exp1 = '(not ((b or not c) and (not a or not c))) or (not (c or not (b and c))) or (a and not c) and (not a or (a and b and c) or (a and ((b and not c) or (not b))))'
exp2 = '(not (a and not b) or (not c and b)) and (not b) or (not a and b and not c) or (a and not b)'


print('exp1:', '\n', eval(translate(exp1)), '\n', to_dnf(eval(translate(exp1)), simplify=True))
print('exp2:', '\n', eval(translate(exp2)), '\n', to_dnf(eval(translate(exp2)), simplify=True))

outputs the simplified expressions

exp1: 
 ~(c | ~(b & c)) | ~((b | ~c) & (~a | ~c)) | (a & ~c & (~a | (a & b & c) | (a & (~b | (b & ~c))))) 
 a | (c & ~b)
exp2: 
 (a & ~b) | (b & ~a & ~c) | (~b & ((b & ~c) | ~(a & ~b))) 
 ~b | (~a & ~c)

Verification of correctness:

for each input, evaluate the truth tables of both the original and the simplified expressions.

def evaluate(a, b, c):
    exp1 = (not ((b or not c) and (not a or not c))) or (not (c or not (b and c))) or (a and not c) and (not a or (a and b and c) or (a and ((b and not c) or (not b))))
    exp2 = (not (a and not b) or (not c and b)) and (not b) or (not a and b and not c) or (a and not b)
    return exp1, exp2

def evaluate2(a, b, c):
    exp1 = a or (c and not b)
    exp2 = not b or (not a and not c)
    return exp1, exp2

values = [(1, 1, 1), (1, 1, 0), (1, 0, 1), (0, 1, 1), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)]
for vals in values:
    print(f'{vals}: {evaluate(*vals)}, {evaluate2(*vals)}')

truth tables output:

(1, 1, 1): (True, False), (1, False)
(1, 1, 0): (True, False), (1, False)
(1, 0, 1): (True, True), (1, True)
(0, 1, 1): (0, 0), (False, False)
(1, 0, 0): (True, True), (1, True)
(0, 1, 0): (0, True), (0, True)
(0, 0, 1): (True, True), (True, True)
(0, 0, 0): (0, True), (0, True)
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80