Advertisement
acclivity

pyYumna

Jan 22nd, 2022
930
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def validate_expr(expr):
  2.     # This function validates the user-suplied expression
  3.     # It returns a validity status (True or False)
  4.     # and also it returns a revised version of the input expression, where references to
  5.     # variable x0 thru x9 are replaced by references to dictionary items xd['x1'] to xd['x9']
  6.     outexpr = ""
  7.  
  8.     # check there are the same number of closing brackets as there are opening brackets
  9.     if (expr.count("(") - expr.count(")")):
  10.         return (False, outexpr)
  11.    
  12.     work = expr         # make a copy of the input expression for working on
  13.     work = work.replace("(", " ")
  14.     work = work.replace(")", " ")
  15.     esplit = work.split()               # split the expression on white space
  16.     for item in esplit:
  17.         # check for a valid variable x0 thru x9
  18.         if item[0].lower() == 'x' and item[1].isdigit() and len(item) == 2:
  19.             continue
  20.         # if not a variable, only "and" and "or" are valid
  21.         if item.lower() in ("and", "or"):
  22.             continue
  23.         return (False, outexpr)
  24.    
  25.     # Process the expression changing all variables x0 to x9 to dictionary references
  26.     j = 0
  27.     while j < len(expr):
  28.         ch = expr[j].lower()
  29.         if ch == 'x':
  30.             outexpr += "xd['x" + expr[j+1] + "']"       # x2 => xd['x2']  etc.
  31.             j += 1
  32.         else:
  33.             outexpr += expr[j]
  34.         j += 1
  35.     return True, outexpr
  36.  
  37. # Define a dictionary with keys 'x0' thru 'x9' (initially with dummy zero values)
  38. xd = {"x0": 0, "x1": 0, "x2": 0, "x3": 0, "x4": 0, "x5": 0, "x6": 0, "x7": 0, "x8": 0, "x9": 0}
  39.  
  40. # a is a list of values to be assigned to xo thru x9
  41. a = [0, 1, 1, -1, -2, 2, 3, -4, 4, 3]
  42.  
  43. # elist is a list of expression for testing this script
  44. elist = ["x1 or x2",                            # OK
  45.          "(x1 and x2) or (x3 and x4) or x5",    # OK
  46.          "X1 and (x2 or x3) or (x4 and x5)",    # OK
  47.          "x1 but not x2",                       # BAD
  48.          "x1 and x2 or (x3",                    # BAD
  49.          "x21 or x9"]                           # BAD
  50.  
  51. for e in elist:
  52.     flag, newexpr = validate_expr(e)
  53.     if not flag:
  54.         print(e, "==> False")
  55.     else:          
  56.         # the expression was syntactically valid
  57.         # Assign values to the dictionary entries 'x0', 'x1' etc.
  58.         for j, v in enumerate(a):
  59.             xd["x" + str(j)] = v
  60.            
  61.         # Evaluate the expression using the values assigned to the dictionary entries
  62.         result = eval(newexpr)
  63.         print(e, " ==> ", newexpr, " ==> ", result)
  64.  
  65. # Results from test data
  66. # x1 or x2  ==>  xd['x1'] or xd['x2']  ==>  1
  67. # (x1 and x2) or (x3 and x4) or x5  ==>  (xd['x1'] and xd['x2']) or (xd['x3'] and xd['x4']) or xd['x5']  ==>  1
  68. # X1 and (x2 or x3) or (x4 and x5)  ==>  xd['x1'] and (xd['x2'] or xd['x3']) or (xd['x4'] and xd['x5'])  ==>  1
  69. # x1 but not x2 ==> False
  70. # x1 and x2 or (x3 ==> False
  71. # x21 or x9 ==> False
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement