Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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"
- binary_bool_dict = {'AND':and_function, 'OR':or_function}
- unary_bool_dict = {'NOT':not_function}
- def interpret( lst, dct):
- if isinstance( lst, list):
- temp_result = "default-false"
- #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:
- try:
- lst[idx+1] = interpret( lst[ idx+1],dct)
- right = lst[idx+1]
- except IndexError:
- return 'Error: right item not found\ndefault-false'
- if item in binary_bool_dict:
- if temp_result == "default-false":
- left = lst[ idx-1]
- else:
- if temp_result == "true" and item == "OR":
- return "true"
- elif temp_result == "false" and item == "AND":
- return "false"
- left = temp_result
- temp_result = binary_bool_dict[item]( left, right)
- else:
- # item in unary_bool_dict:
- temp_result = unary_bool_dict[item](right)
- 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'
- print (interpret( ["NOT", "true"], {"NOT": "false"})) # default-false
- print (interpret( ["NOT", "true","AND"], {"NOT": "false"})) # Error: right item not found default-false
- print (interpret( ["NOT", "true","AND","true"], {"AND":"true"})) # false
- '''
- >>> eval('not True and True')
- False
- >>> not = False
- File "<stdin>", line 1
- not = False
- ^
- SyntaxError: invalid syntax
- >>> eval('False True and True')
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- File "<string>", line 1
- False True and True
- ^
- SyntaxError: invalid syntax
- >>>
- print (interpret( ["NOT", "true","AND","true"], {"NOT": "false"}))
- # true, because the latest temp_result value is true( from AND eval) and at this point lst --> ["false","true"]
- # so list passed has invalid structure
- '''
Add Comment
Please, Sign In to add comment