逻辑值解释函数返回不正确的结果

编程入门 行业动态 更新时间:2024-10-19 14:44:27
本文介绍了逻辑值解释函数返回不正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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)

推荐答案

上面发布的代码的问题在于它不处理子表达式,您似乎在示例中允许这样做.根据设计,您似乎只允许二进制AND和OR,以及一元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.

更多推荐

逻辑值解释函数返回不正确的结果

本文发布于:2023-10-26 18:47:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正确   函数   逻辑

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!