codextj

recursive_call_explanation

Oct 1st, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 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. def interpret( exprsn, dct):
  26.  
  27.     if isinstance( exprsn, list):
  28.         lst = exprsn
  29.         temp_result = None
  30.        
  31.         for idx, item in enumerate( lst):
  32.            
  33.             if item == 'true' or item == 'false':
  34.                 # item is already in its interpreted state
  35.                 continue
  36.  
  37.             elif isinstance( item, list):
  38.                 # recursive call cond 2)
  39.                 lst[idx] = interpret( item,dct)
  40.  
  41.             elif item in dct:
  42.                 # get the meaning of this item from dictionary
  43.                 lst[idx] = dct[item]
  44.  
  45.             else:
  46.                 # recursive call cond 1)
  47.                 lst[idx+1] = interpret( lst[ idx+1],dct)
  48.                
  49.                 '''
  50.                interpret(["NOT", ["lights_on", "AND" , "candleNeeded"]],{"lights_on":"true", "candleNeeded" :"false"})
  51.                temp_result = None
  52.                
  53.                item --> "NOT" ie item is in unary_bool_dict
  54.                
  55.                NOT requires operand on right side to be interpreted completely first ( we can do short-circuit-eval for OR, AND see the improved version of answer)
  56.                
  57.                next item( right operand) is a list so we will make a recursive call( cond 2)
  58.                
  59.                { lst[idx+1] }{1} = interpret( { lst[ idx+1] }{2}, {dct}{3} ) after finding interpretation of list we will replace with its interpretation
  60.                
  61.                ["lights_on", "AND" , "candleNeeded"]{1} = interpret( ["lights_on", "AND" , "candleNeeded"]{2},{lights_on:"true", candleNeeded: "false"}{3} )
  62.                
  63.                lst[idx+1] = "false"
  64.                
  65.                now our orignal list arguement has become = ["NOT","false"]
  66.                
  67.                temp_result = unary_bool_dict[item]("false")
  68.                
  69.                temp_result = "true"
  70.                
  71.                '''
  72.  
  73.                 if item in binary_bool_dict:
  74.  
  75.                     if temp_result == None:
  76.                         temp_result = binary_bool_dict[item]( lst[ idx-1], lst[idx+1])
  77.                     else:
  78.                         temp_result = binary_bool_dict[item]( temp_result, lst[idx+1])
  79.  
  80.                 else:
  81.                     # item in unary_bool_dict:
  82.                     temp_result  = unary_bool_dict[item](lst[idx+1])
  83.  
  84.         return temp_result # eventually temp_result will become our final answer
  85.  
  86.     else:
  87.         return dct.get( exprsn,exprsn) # if exprsn( as key) is present in dct get its value else just return exprsn ( it must be already a 'true' or 'false')
  88.        
  89.        
  90. print(interpret(["NOT", ["lights_on", "AND" , "candleNeeded"]],{"lights_on":"true", "candleNeeded" :"false"}))
Add Comment
Please, Sign In to add comment