Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Evaluate mathematical expression

# Python3 program to evaluate a given
# expression where tokens are
# separated by space.
 
# Function to find precedence
# of operators.
def precedence(op):
     
    if op == '+' or op == '-':
        return 1
    if op == '*' or op == '/':
        return 2
    return 0
 
# Function to perform arithmetic
# operations.
def applyOp(a, b, op):
     
    if op == '+': return a + b
    if op == '-': return a - b
    if op == '*': return a * b
    if op == '/': return a // b
 
# Function that returns value of
# expression after evaluation.
def evaluate(tokens):
     
    # stack to store integer values.
    values = []
     
    # stack to store operators.
    ops = []
    i = 0
     
    while i < len(tokens):
         
        # Current token is a whitespace,
        # skip it.
        if tokens[i] == ' ':
            i += 1
            continue
         
        # Current token is an opening
        # brace, push it to 'ops'
        elif tokens[i] == '(':
            ops.append(tokens[i])
         
        # Current token is a number, push
        # it to stack for numbers.
        elif tokens[i].isdigit():
            val = 0
             
            # There may be more than one
            # digits in the number.
            while (i < len(tokens) and
                tokens[i].isdigit()):
             
                val = (val * 10) + int(tokens[i])
                i += 1
             
            values.append(val)
             
            # right now the i points to
            # the character next to the digit,
            # since the for loop also increases
            # the i, we would skip one
            #  token position; we need to
            # decrease the value of i by 1 to
            # correct the offset.
              i-=1
         
        # Closing brace encountered,
        # solve entire brace.
        elif tokens[i] == ')':
         
            while len(ops) != 0 and ops[-1] != '(':
             
                val2 = values.pop()
                val1 = values.pop()
                op = ops.pop()
                 
                values.append(applyOp(val1, val2, op))
             
            # pop opening brace.
            ops.pop()
         
        # Current token is an operator.
        else:
         
            # While top of 'ops' has same or
            # greater precedence to current
            # token, which is an operator.
            # Apply operator on top of 'ops'
            # to top two elements in values stack.
            while (len(ops) != 0 and
                precedence(ops[-1]) >=
                   precedence(tokens[i])):
                         
                val2 = values.pop()
                val1 = values.pop()
                op = ops.pop()
                 
                values.append(applyOp(val1, val2, op))
             
            # Push current token to 'ops'.
            ops.append(tokens[i])
         
        i += 1
     
    # Entire expression has been parsed
    # at this point, apply remaining ops
    # to remaining values.
    while len(ops) != 0:
         
        val2 = values.pop()
        val1 = values.pop()
        op = ops.pop()
                 
        values.append(applyOp(val1, val2, op))
     
    # Top of 'values' contains result,
    # return it.
    return values[-1]
 
# Driver Code
if __name__ == "__main__":
     
    print(evaluate("10 + 2 * 6"))
    print(evaluate("100 * 2 + 12"))
    print(evaluate("100 * ( 2 + 12 )"))
    print(evaluate("100 * ( 2 + 12 ) / 14"))
 
# This code is contributed
# by Rituraj Jain
Comment

PREVIOUS NEXT
Code Example
Python :: Retry function for sending data 
Python :: walrus with ternary python 
Python :: check if id is present in elasticsearch using python 
Python :: python without creating pyc 
Python :: self.stdout.write django 
Python :: discord.py main file setup 
Python :: replace string in dictionary python 
Python :: ring Creating a Multi-Dimensional Array using List 
Python :: ring Using This in the class region as Self 
Python :: text to ascii art generator python 
Python :: ring Trace Library 
Python :: Lambda expressions using f-string 
Python :: how to write stuff in python 
Python :: get feature names from one hot encoder 
Python :: dic to dic arrays must all be same length 
Python :: python launch ipython from script 
Python :: funtools rougly equivalent to, internal 
Python :: trello class 
Python :: gensim wordvector vocabulary list 
Python :: how to create customer using python api of shopify 
Python :: Shelve Data Storage 
Python :: pls work 
Python :: multiplication table in python 
Python :: how to make an instagram report bot python 
Python :: how to run django server outside world 
Python :: How to call any function with it name as a string 
Python :: move to next iteration of for loop python 
Python :: _set.filter django 
Python :: Take input of any number and generate all possible binary strings without recursion 
Python :: Display summary of all the numerical variables in the DataFrame 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =