Solve the Logical Expression given by string | Python3 program to solve the logical expression . ; Function to evaluate the logical expression ; traversing string from the end . ; for NOT operation ; for AND and OR operation ; Driver code
import math as mt NEW_LINE def logicalExpressionEvaluation ( string ) : NEW_LINE INDENT arr = list ( ) NEW_LINE n = len ( string ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( string [ i ] == " [ " ) : NEW_LINE INDENT s = list ( ) NEW_LINE while ( arr [ - 1 ] != " ] " ) : NEW_LINE INDENT s . append ( arr [ - 1 ] ) NEW_LINE arr . pop ( ) NEW_LINE DEDENT arr . pop ( ) NEW_LINE if ( len ( s ) == 3 ) : NEW_LINE INDENT if s [ 2 ] == "1" : NEW_LINE INDENT arr . append ( "0" ) NEW_LINE DEDENT else : NEW_LINE INDENT arr . append ( "1" ) NEW_LINE DEDENT DEDENT elif ( len ( s ) == 5 ) : NEW_LINE INDENT a = int ( s [ 0 ] ) - 48 NEW_LINE b = int ( s [ 4 ] ) - 48 NEW_LINE c = 0 NEW_LINE if s [ 2 ] == " & " : NEW_LINE INDENT c = a & b NEW_LINE DEDENT else : NEW_LINE INDENT c = a | b NEW_LINE DEDENT arr . append ( ( c ) + 48 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT arr . append ( string [ i ] ) NEW_LINE DEDENT DEDENT return arr [ - 1 ] NEW_LINE DEDENT string = " [ | , [ & ,1 , [ ! , 0 ] ] , [ ! , [ | , [ ▁ , 1,0 ] , [ ! ,1 ] ] ] ] " NEW_LINE print ( logicalExpressionEvaluation ( string ) ) NEW_LINE