codextj

short_circuit_eval_version

Oct 1st, 2018
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 KB | None | 0 0
  1. def and_function(expression1, expression2):
  2.     if expression1=="true" and expression2=="true":
  3.         return "true"
  4.     else:
  5.         return "false"
  6.  
  7.  
  8. def or_function(expression1, expression2):
  9.     if expression1== "true" or expression2=="true":
  10.         return "true"
  11.     else:
  12.         return "false"
  13.  
  14.  
  15. def not_function(expression):
  16.     if expression == "true":
  17.         return "false"
  18.     elif expression == "false":
  19.         return "true"
  20.        
  21.        
  22. binary_bool_dict = {'AND':and_function, 'OR':or_function}
  23. unary_bool_dict = {'NOT':not_function}
  24.  
  25.  
  26.  
  27.  
  28. def interpret( lst, dct):
  29.  
  30.     if isinstance( lst, list):
  31.         temp_result = "default-false"
  32.         #print( lst)
  33.         for idx, item in enumerate( lst):
  34.             #print( idx,'item',item)
  35.             if item == 'true' or item == 'false':
  36.                 continue
  37.  
  38.             elif isinstance( item, list):
  39.                 lst[idx] = interpret( item,dct)
  40.  
  41.             elif item in dct:
  42.                 lst[idx] = dct[item]
  43.  
  44.             else:
  45.                 try:
  46.                     lst[idx+1] = interpret( lst[ idx+1],dct)
  47.                     right = lst[idx+1]
  48.                    
  49.                 except IndexError:
  50.                     return 'Error: right item not found\ndefault-false'
  51.                
  52.                 if item in binary_bool_dict:
  53.  
  54.                     if temp_result ==  "default-false":
  55.                         left = lst[ idx-1]
  56.                     else:
  57.                         if temp_result == "true" and item == "OR":
  58.                             return "true"
  59.                            
  60.                         elif temp_result == "false" and item == "AND":
  61.                             return "false"
  62.                            
  63.                         left = temp_result
  64.                    
  65.                     temp_result = binary_bool_dict[item]( left, right)
  66.  
  67.                 else:
  68.                     # item in unary_bool_dict:
  69.  
  70.                     temp_result  = unary_bool_dict[item](right)
  71.  
  72.         return temp_result # eventually temp_result will become our final answer
  73.  
  74.     else:
  75.         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')
  76.        
  77.  
  78.  
  79. print(interpret(["door_open", "AND", "cat_gone"],
  80.                {"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"} )) #'false'
  81.  
  82. print(interpret(["cat_asleep", "OR", ["NOT", "cat_gone"]],
  83.                {"door_open" : "false", "cat_gone" : "true", "cat_asleep" : "true"})) #'true'
  84.  
  85. print(interpret(["true", "OR", "true"], {}))  #'true'
  86.  
  87. print(interpret("cat_gone", {"door_open": "false", "cat_gone": "true"})) #'true'
  88.  
  89. print(interpret(["NOT", ["NOT", ["NOT", ["cat_asleep", "OR", ["NOT", "cat_asleep"]]]]],
  90.                {"cat_asleep": "false"})) #'false'
  91.  
  92. print(interpret(["NOT", "AND", "true"], {"NOT":"true"})) #'true'
  93. print(interpret(["NOT", "AND"], {"AND": "false"})) #'true'
  94.  
  95.  
  96.  
  97.  
  98. print (interpret( ["NOT", "true"], {"NOT": "false"})) # default-false
  99. print (interpret( ["NOT", "true","AND"], {"NOT": "false"})) # Error: right item not found default-false
  100. print (interpret( ["NOT", "true","AND","true"], {"AND":"true"})) # false
  101.  
  102.  
  103.  
  104.  
  105.  
  106. '''
  107. >>> eval('not True and True')
  108. False
  109. >>> not = False
  110.  File "<stdin>", line 1
  111.    not = False
  112.        ^
  113. SyntaxError: invalid syntax
  114. >>> eval('False True and True')
  115. Traceback (most recent call last):
  116.  File "<stdin>", line 1, in <module>
  117.  File "<string>", line 1
  118.    False True and True
  119.             ^
  120. SyntaxError: invalid syntax
  121. >>>
  122.  
  123. print (interpret( ["NOT", "true","AND","true"], {"NOT": "false"}))
  124.  
  125. # true, because the latest temp_result value is true( from AND eval) and at this point lst --> ["false","true"]
  126. # so list passed has invalid structure
  127.  
  128. '''
Add Comment
Please, Sign In to add comment