命题逻辑以解释两个论点 [英] Propositional logic in order to interpret two arguments

查看:58
本文介绍了命题逻辑以解释两个论点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,它接受两个参数(一个列表和一个字典)并返回输出,例如:

>>>interpret(["door_open", "AND", "cat_gone"],{"door_open": "false", "cat_gone": "true", "cat_asleep": "true"})'错误的'

interpret(["cat_asleep", "OR", ["NOT", "cat_gone"]],{"door_open": "false", "cat_gone": "true", "cat_asleep": "true"})'真的'

.....................................……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………….....

因为我想在这个特定函数中使用尽可能少的逻辑语句,所以我创建了几个单独的函数来代替:

def and_function(expression1, expression2):如果 expression1=="true" 和 expression2=="true":返回真"别的:返回假"def or_function(expression1, expression2):如果表达式1==真"或表达式2==真":返回真"别的:返回假"def not_function(表达式):如果表达式==真":返回假"elif 表达式 == 假":返回真"

.....................................…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………….....

然而,现在我被卡住了一段时间,因为我不太明白程序是如何首先遍历列表和嵌套列表的.然后通过字典挑出与键(在列表中)相关的值,然后将列表中的值与逻辑语句一起进行比较.我知道为了遍历嵌套列表可能需要某种形式的递归,但无法真正弄清楚如何将它们组合在一起.

关于如何解决此类问题的任何想法?

解决方案

改进版的答案这里.

这里没有进行异常处理,所以我认为如果它保证传递的列表(参数)具有有效的结构,例如 OR,AND 运算符(如果列表中存在)应该在两侧都有操作数,那么我认为代码会正常工作.

.....................................……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………......

您编写的辅助函数将通过此 dicts 调用.

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

.....................................……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………......

使用递归我们可以解决这个问题,每当 1) 我们还没有解释项目时,就像在执行 AND 函数调用时一样,我们从右侧获取项目,即我们还没有访问过的项目(因为我们从左到右遍历,即从索引 0 --> len(list)) 或 2) item 是一个列表,我们将进行递归调用.>

function interpret 将返回 'true''false'

def解释(lst,dct):if isinstance(lst, list):temp_result = 无#print(lst)对于 idx,枚举项(lst):#print(idx,'item',item)如果 item == 'true' 或 item == 'false':继续elif isinstance(项目,列表):lst[idx] = 解释(项目,dct)dct 中的 elif 项目:lst[idx] = dct[项目]别的:lst[idx+1] = interpret(lst[idx+1],dct)如果 binary_bool_dict 中的项目:如果 temp_result == 无:temp_result = binary_bool_dict[item](lst[idx-1],lst[idx+1])别的:temp_result = binary_bool_dict[item](temp_result, lst[idx+1])别的:# unary_bool_dict 中的项目:temp_result = unary_bool_dict[item](lst[idx+1])return temp_result # 最终 temp_result 将成为我们的最终答案别的:return dct.get(lst,lst) # if key: lst is present in dct get its value else just return lst (它必须已经是真"或假")

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

print(interpret(["door_open", "AND", "cat_gone"],{"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"} )) #'false'打印(解释([cat_asleep",或",[NOT",cat_gone"]],{"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"})) #'true'打印(解释([真",或",真"],{}))#'真'print(interpret("cat_gone", {"door_open": "false", "cat_gone": "true"})) #'true'打印(解释([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'

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", "cat_gone"], 
           {"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"})

'false'    

or

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

'true'

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

As I want to use as few logical statements as possible in this particular function, I have created a couple of separate functions that will do this instead:

def and_function(expression1, expression2):
    if expression1=="true" and expression2=="true":
        return "true"
    else:
        return "false"


def or_function(expression1, expression2):
    if expression1== "true" or expression2=="true":
        return "true"
    else:
        return "false"


def not_function(expression):
    if expression == "true":
        return "false"
    elif expression == "false":
        return "true"

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

However, now I have been stuck for sometime, as I don't quite understand how the program is suppose to first go through the list and nested lists. Then go through the dictionary pick out the value related to the key (which is in the list) and then put compare the values in the list to each other together with the logical statement. I get that recursion of some form is probably needed in order to go through the nested lists, but can't really figure out how to put it all together.

Any ideas on how to solve this kind of problem?

解决方案

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'

def interpret( lst, dct):

    if isinstance( lst, list):
        temp_result = None 
        #print( lst)
        for idx, item in enumerate( lst):
            #print( idx,'item',item)
            if item == 'true' or item == 'false':
                continue

            elif isinstance( 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 answer

    else:
        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'

这篇关于命题逻辑以解释两个论点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆