逻辑值解释函数返回不正确的结果 [英] Logical value interpretation function returning incorrect results

查看:126
本文介绍了逻辑值解释函数返回不正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是编写一个函数解释,它接受两个参数,一个有效的表达式和一个解释,并且如果整个表达式在此特定解释中为真,则给出真值.逻辑常量和表达式应作为字符串进行管理,因此您需要为布尔运算实现自己的函数.

My task is to write a function interpret that takes two arguments, a valid expression and an interpretation, and gives the truth value if the entire expression is true in this particular interpretation. The logical constants and expressions should be managed as strings, so you need to implement their own functions for the Boolean operations.

请注意,该函数应返回"true"或"false",而不是True或False.

Note that the function should return "true" or "false" not True or False.

以下是一些可能的输入和输出:

Here are some possible inputs and outputs:

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

所以这是我的代码,它适用于上面显示的第一种和最后一种情况,但是在所有其他情况下,我要么得到错误的输出,要么根本没有得到.这是什么问题,我该如何解决?

So this is my code down here, it works for the first and last case shown above but in all others I either get the wrong output or none at all. Whats is the problem here and how do I fix it?

def and_operator(statement, values):
    if isinstance(statement[0], str) and isinstance(statement[2], str):
        statement[0] = (statement[0], values)
        statement[2] = (statement[2], values)
        return "true" if statement[0] == "true" and statement[2] == "true" else "false"

    if isinstance(statement[0], str):
        return and_operator()
    return interpret(statement[0], values), interpret(statement[2], values)


def or_operator(statement, values):
    if isinstance(statement[0], str) and isinstance(statement[2], str):
        statement[0] = (statement[0], values)
        statement[2] = (statement[2], values)
        return "true" if statement[0] == "true" or statement[2] == "true" else "false"


def not_operator(expr, value):
    if isinstance(expr[1], str):
        return "false" if expr[1] == "true" else "true"
    return interpret(expr[1:], value)


def real_values(element, interp):
    if element in interp:
        element = interp[element]
        return element


def item_is_list(e, i):
    return True if isinstance(e[i], list) else False


def interpret(statement, interpretation):
    length = len(statement)
    if item_is_list(statement, 0):
        return statement

    if length == 3:
        if statement[1] == "AND":
            return and_operator(statement, interpretation)
        elif statement[1] == "OR":
            return or_operator(statement, interpretation)
    elif len(statement) == 2:
        return not_operator(statement, interpretation)

推荐答案

上面发布的代码的问题在于它不处理子表达式,您似乎在示例中允许这样做.根据设计,您似乎只允许二进制ANDOR,以及一元NOT.但是,未实现and_operator,也无法解决子表达式(通过递归调用interpret()并处理其返回的值).其他功能(例如real_values)无用.最后,必须检查输入表达式和字典的句法有效性.

The problem with the code posted above is that it doesn't handle subexpressions, which you seem to allow in the examples. You seem to only allow binary AND and OR, and unary NOT, by design. However, and_operator is not implemented, and subexpressions are not solved (by recursively calling interpret() and handling the value it returns). Other functions such as real_values have no use. Finally, the syntactic validity of input expressions and dictionaries has to be checked.

如果您想了解口译的工作原理,则最好遵循,或.否则,如果您只需要该功能可靠地工作,则可以利用现有的经过测试的代码,例如解析 pyparsing ,甚至是

If you want to understand how interpreting works, you'd better follow some tutorial like this, this, or this. Otherwise, if you just need that function to work reliably, you may take advantage of existing, tested code, such as parse, pyparsing, or even parser.

这篇关于逻辑值解释函数返回不正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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