Skip to content Skip to sidebar Skip to footer

Propositional Logic In Order To Interpret Two Arguments

I'm trying to write a function that will take in two arguments (one list and one dictionary) and return the output for instance, like so: >>>interpret(['door_open', 'AND',

Solution 1:

You could split the operators out into their own functions, but I'm not sure how much clarity that adds when there are only three. I wrote the actual function to work with booleans, because I think it ends up a bit cleaner.

definterpret(conditions, states):
    return"true"if _interpret(
        conditions,
        {k: v == "true"for k, v in states.items()},
    ) else"false"def_interpret(conditions, states):
    # NOT is a special case; just one argument in position 1if conditions[0] == "NOT":
        iftype(conditions[1]) == list:
            value = _interpret(conditions[1], states)
        else:
            value = states[conditions[1]]

        returnnot value

    iftype(conditions[0]) == list:
        first = _interpret(conditions[0], states)
    else:
        first = states[conditions[0]]

    iftype(conditions[2]) == list:
        second = _interpret(conditions[2], states)
    else:
        second = states[conditions[2]]

    return (first and second) if (conditions[1] == "AND") else (first or second)

Shortening that up a bit:

importoperator

def _interpret(c, s):
    if c[0] == "NOT":
        return not (_interpret(c[1], s) if (type(c[1]) == list) else s[c[1]])

    return (operator.and_ if (c[1] == "AND") elseoperator.or_)(
        _interpret(c[0], s) if (type(c[0]) == list) else s[c[0]],
        _interpret(c[2], s) if (type(c[2]) == list) else s[c[2]],
    )

Solution 2:

improved version of answer here.

There is no exception handling done here, so I think code will work just fine if its guaranteed that list ( argument) passed has valid structure for example OR, AND operator if present in list should have operand on both the sides.

..........................................................................................................................................................

helper functions that you have written will be called through this dicts.

binary_bool_dict = {'AND':and_function, 'OR':or_function}
unary_bool_dict = {'NOT':not_function}

..........................................................................................................................................................

Using recursion we can solve this problem, whenever 1) we have not interpreted the item like when doing AND function call we are getting item from right hand side ie item which we have not visited yet( since we traverse from left to right ie from index 0 --> len(list)) or 2) item is a list we will make a recursive call.

function interpret will return 'true' or 'false'

definterpret( lst, dct):

    ifisinstance( lst, list):
        temp_result = None#print( lst)for idx, item inenumerate( lst):
            #print( idx,'item',item)if item == 'true'or item == 'false':
                continueelifisinstance( item, list):
                lst[idx] = interpret( item,dct)

            elif item in dct:
                lst[idx] = dct[item]

            else:
                lst[idx+1] = interpret( lst[ idx+1],dct)

                if item in binary_bool_dict:

                    if temp_result == None:
                        temp_result = binary_bool_dict[item]( lst[ idx-1], lst[idx+1])
                    else:
                        temp_result = binary_bool_dict[item]( temp_result, lst[idx+1])

                else:
                    # item in unary_bool_dict:

                    temp_result  = unary_bool_dict[item](lst[idx+1])

        return temp_result # eventually temp_result will become our final answerelse:
        return dct.get( lst,lst) # if key: lst is present in dct get its value else just return lst ( it must be already a 'true' or 'false')

................................................................................................................................................................

print(interpret(["door_open", "AND", "cat_gone"], 
               {"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"} )) #'false'print(interpret(["cat_asleep", "OR", ["NOT", "cat_gone"]],
               {"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"})) #'true'print(interpret(["true", "OR", "true"], {}))  #'true'print(interpret("cat_gone", {"door_open": "false", "cat_gone": "true"})) #'true'print(interpret(["NOT", ["NOT", ["NOT", ["cat_asleep", "OR", ["NOT", "cat_asleep"]]]]],
               {"cat_asleep": "false"})) #'false'print(interpret(["NOT", "AND", "true"], {"NOT":"true"})) #'true'print(interpret(["NOT", "AND"], {"AND": "false"})) #'true'

Post a Comment for "Propositional Logic In Order To Interpret Two Arguments"