Remove redundant parentheses from an arithmetic expression

后端 未结 7 1267
陌清茗
陌清茗 2020-12-13 10:27

This is an interview question, for which I did not find any satisfactory answers on stackoverflow or outside. Problem statement:

Given an arithmetic

相关标签:
7条回答
  • 2020-12-13 11:11

    This solutions works if the expression is a valid. We need mapping of the operators to priority values.

    a. Traverse from two ends of the array to figure out matching parenthesis from both ends. Let the indexes be i and j respectively.

    b. Now traverse from i to j and find out the lowest precedence operator which is not contained inside any parentheses.

    c. Compare the priority of this operator with the operators to left of open parenthesis and right of closing parenthesis. If no such operator exists, treat its priority as -1. If the priority of the operator is higher than these two, remove the parenthesis at i and j.

    d. Continue the steps a to c until i<=j.

    0 讨论(0)
  • 2020-12-13 11:15

    Just in case someone finds this question while looking for a quick and easy solution: If your expression is sanitized and your language (or library) provides an eval function for your expression, you could just try if removing a pair of brackets will change the value of your expression. If it does, you'll have to keep the brackets. If it does not, you can remove them.

    However, keep in mind, this is not an efficient solution, of course, but rather the "brute force" path.

    Here's an exemplary implementation in Python which works for integers and the built-in eval:

    def remove_brackets(term):
        a = 0
        while True:
            # Find opening bracket
            try:
                a = term.index("(", a)
            except ValueError:
                # No (more) opening brackets found
                break
            # Find corresponding closing bracket
            b = a
            while True:
                b = term.index(")", b + 1)
                if term[a + 1:b].count("(") == term[a + 1:b].count(")"):
                    break
            # Assemble new term by removing current pair of brackets
            new_term = term[:a] + term[a + 1:b] + term[b + 1:]
            # If new term produces a different value, keep term as it is and try with the next pair of brackets
            if eval(term) != eval(new_term):
                a += 1
                continue
            # Adopt new term
            term = new_term
        return term
    

    Example calls:

    >>> remove_brackets("1 + (2 * 3)")
    '1 + 2 * 3'
    >>> remove_brackets("1 + (2 * 3) / 4")
    '1 + 2 * 3 / 4'
    >>> remove_brackets("1 + (2 * 3) / 4 / (5 * 6)")
    '1 + 2 * 3 / 4 / (5 * 6)'
    
    0 讨论(0)
  • 2020-12-13 11:21
    1. Push one empty item in the stack
    2. Scan the token list

      2.1 if the token is operand, ignore.

      2.2 if the token is operator, records the operator in the left_op, if min_op is nil, set the min_op = this operator, if the min_op is not nil, compare the min_op with this operator, set min_op as one of the two operators with less priority.

      2.3 if the token is left parenthese, push one item in the stack, with left_pa = position of the parenthesis.

      2.4 if the token is right parenthesis:

      2.4.1 we have the pair of the parentheses(left_pa and the right parenthesis)

      2.4.2 pop the item

      2.4.3 pre-read next token, if it is an operator, set it as right operator

      2.4.4 compare min_op of the item with left_op and right operator (if any of them exists), we can easily get to know if the pair of the parentheses is redundant, and output it(if the min_op < any of left_op and right operator, the parentheses are necessary, if min_op = left_op, the parentheses are necessary, otherwise redundant)

      2.4.5 if there is no left_op and no right operator(which also means min_op = nil) and the stack is not empty, set the min_op of top item as the min_op of the popped-up item examples

    0 讨论(0)
  • 2020-12-13 11:22

    A pair of parentheses is necessary if and only if they enclose an unparenthesized expression of the form X % X % ... % X where X are either parenthesized expressions or atoms, and % are binary operators, and if at least one of the operators % has lower precedence than an operator attached directly to the parenthesized expression on either side of it; or if it is the whole expression. So e.g. in

    q * (a * b * c * d) + c
    

    the surrounding operators are {+, *} and the lowest precedence operator inside the parentheses is *, so the parentheses are unnecessary. On the other hand, in

    q * (a * b + c * d) + c
    

    there is a lower precedence operator + inside the parentheses than the surrounding operator *, so they are necessary. However, in

    z * q + (a * b + c * d) + c
    

    the parentheses are not necessary because the outer * is not attached to the parenthesized expression.

    Why this is true is that if all the operators inside an expression (X % X % ... % X) have higher priority than a surrounding operator, then the inner operators are anyway calculated out first even if the parentheses are removed.

    So, you can check any pair of matching parentheses directly for redundancy by this algorithm:

    Let L be operator immediately left of the left parenthesis, or nil
    Let R be operator immediately right of the right parenthesis, or nil
    If L is nil and R is nil:
      Redundant
    Else:
      Scan the unparenthesized operators between the parentheses
      Let X be the lowest priority operator
      If X has lower priority than L or R:
        Not redundant
      Else:
        Redundant
    

    You can iterate this, removing redundant pairs until all remaining pairs are non-redundant.

    Example:

    ((a * b) + c * (e + f))
    

    (Processing pairs from left to right):

    ((a * b) + c * (e + f))   L = nil R = nil --> Redundant
    ^                     ^   
     (a * b) + c * (e + f)    L = nil R = nil --> Redundant
     ^     ^                  L = nil R = + X = * --> Redundant
      a * b  + c * (e + f)    L = * R = nil X = + --> Not redundant
                   ^     ^
    

    Final result:

    a * b + c * (e + f)
    
    0 讨论(0)
  • 2020-12-13 11:24

    I just figured out an answer:

    the premises are:

    1. the expression has been tokenized
    2. no syntax error
    3. there are only binary operators
    

    input:

    list of the tokens, for example:
       (, (, a, *, b, ), +, c, )
    

    output:

    set of the redundant parentheses pairs (the orders of the pairs are not important),
    for example,
       0, 8
       1, 5
    

    please be aware of that : the set is not unique, for instance, ((a+b))*c, we can remove outer parentheses or inner one, but the final expression is unique

    the data structure:

    a stack, each item records information in each parenthese pair
    the struct is:
       left_pa: records the position of the left parenthese
       min_op: records the operator in the parentheses with minimum priority
       left_op: records current operator
    

    the algorithm

    1.push one empty item in the stack
    2.scan the token list
        2.1 if the token is operand, ignore
        2.2 if the token is operator, records the operator in the left_op, 
            if min_op is nil, set the min_op = this operator, if the min_op 
            is not nil, compare the min_op with this operator, set min_op as 
            one of the two operators with less priority
        2.3 if the token is left parenthese, push one item in the stack, 
            with left_pa = position of the parenthese
        2.4 if the token is right parenthese, 
            2.4.1 we have the pair of the parentheses(left_pa and the 
                 right parenthese)
            2.4.2 pop the item
            2.4.3 pre-read next token, if it is an operator, set it 
                 as right operator
            2.4.4 compare min_op of the item with left_op and right operator
                 (if any of them exists), we can easily get to know if the pair 
                 of the parentheses is redundant, and output it(if the min_op
                 < any of left_op and right operator, the parentheses are necessary,
                 if min_op = left_op, the parentheses are necessary, otherwise
                 redundant) 
            2.4.5 if there is no left_op and no right operator(which also means 
                 min_op = nil) and the stack is not empty, set the min_op of top 
                 item as the min_op of the popped-up item
    

    examples

    example one

    ((a*b)+c)
    

    after scanning to b, we have stack:

    index left_pa min_op left_op
    0
    1     0       
    2     1       *      *       <-stack top
    

    now we meet the first ')'(at pos 5), we pop the item

    left_pa = 1 
    min_op = *
    left_op = *
    

    and pre-read operator '+', since min_op priority '*' > '+', so the pair(1,5) is redundant, so output it. then scan till we meet last ')', at the moment, we have stack

    index left_pa min_op left_op
    0
    1     0       +      + 
    

    we pop this item(since we meet ')' at pos 8), and pre-read next operator, since there is no operator and at index 0, there is no left_op, so output the pair(0, 8)

    example two

    a*(b+c)
    

    when we meet the ')', the stack is like:

    index  left_pa  min_op left_op
    0               *      *
    1      2        +      +
    

    now, we pop the item at index = 1, compare the min_op '+' with the left_op '*' at index 0, we can find out the '(',')' are necessary

    0 讨论(0)
  • 2020-12-13 11:33

    The below code is a straightforward solution, limited to +-*/; if you want you can add them per your requirements.

    #include <iostream>
    #include <stack>
    #include <set>
    using namespace std;
    
    int size;
    int loc;
    set<char> support;
    string parser(string input , int _loc){
    
        string expi;
        set<char> op;
        loc = _loc;
    
        while(1){
            if(input[loc] ==  '('){
                expi += parser(input,loc+1);
            }else if(input[loc] == ')'){
              if((input[loc+1] != '*') && (input[loc+1] != '/')){
                  return expi;
              }else{
                  if ((op.find('+') == op.end()) && (op.find('-') == op.end())){
                      return expi;
                  }else{
                      return '('+expi+')';
                  }
              }
            }else{
                char temp = input[loc];
                expi=expi+temp;
                if(support.find(temp) != support.end()){
                    op.insert(temp);
                }
            }
            loc++;
            if(loc >= size){
                break;
            }
        }
    
        return expi;
    }
    
    int main(){
        support.insert('+');
        support.insert('-');
        support.insert('*');
        support.insert('/');
    
        string input("(((a)+((b*c)))+(d*(f*g)))");
        //cin >> input;
        size = input.size();
    
        cout<<parser(input,0);
    
        return 0;
    }       
    
    0 讨论(0)
提交回复
热议问题