pythonpython-3.xpython-importsympycnf

Change position of input to call a function in python


This is my code :

from sympy import symbols, Equivalent
from sympy.logic.boolalg import to_cnf as fnc, Implies, to_cnf
from sympy.abc import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, v, w, x, y, z


def eq(a, b):
    return fnc(Equivalent(a, b), True)

inr = str(input('Enter formula: '))
x = ' '
for i in inr:
     #print(inr[inr.index(i-2)])
     if i == '>':
         i = '>>'
         x += i
     elif i == '=':

         print(i)
         i = 'Equivalent'
         x += i

     else:
         x += i

print(fnc(x, True))

Instead of input =(a,b) to call Equivalent function i want to input (a=b). How can I do that? I have tried to controlling the element before and next of = and add it to Equivalent function when you enter (a=b) the output should be (a|~b)&(b|~a) but it does not work.

i= sympy.Equivalent(symbols((inr[inr.index(i) - 1])), symbols(inr[inr.index(i) + 1]))

Solution

  • So, based on your Sample I/O which was:

    IN: so when you enter (a=b) the output should be? – user5173426

    OUT: it should be (a | ~b) & (b | ~a) – Yacine Benatia

    I made a few changes to your existing code:

    from sympy import symbols, Equivalent
    from sympy.logic.boolalg import to_cnf as fnc, Implies, to_cnf
    from sympy.abc import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, v, w, x, y, z
    
    
    def eq(a, b):
        return fnc(Equivalent(a, b), True)
    
    inr = str(input('Enter formula: '))
    x = ' '
    # get the part of the formula/string before =
    y = inr.rsplit('=', 1)[0]
    # get the part of the formula/string after =
    z = inr.rsplit('=', 1)[1]
    for i in inr:
         if i == '>':
             i = '>>'
             x += i
         elif i == '=':
             i = 'Equivalent'
             x = i
         elif i != ')':
             x += y
             x += ','
             x +=  z
         # print(x)
    
    print(fnc(x, True))
    

    OUTPUT:

    OUT