问题
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.
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)
回答1:
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.
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.
来源:https://stackoverflow.com/questions/46621733/logical-value-interpretation-function-returning-incorrect-results