Python Mathematical Formula computation
Question: I'm writing a python program to calculate formula. I read in a list of strings
which hold the values, operators and functions.

The code shown below receives a string for example:

    
    
    ['not', 1.0, 2.0, '=', 'power', 2.0, 3.0, '+']
    

The code above is a postfix version of the mathematical problem:
power(2,3)+not(2=1) The program should calculate not(2=1) first, resulting in
1 and then calculate power(2,3) giving 9 and then 8+0 resulting in a return of
8.

So far my code to calculate the answers

    
    
    stack = []
        def calculate(inputs): 
            if (inputs[0] == "sum"):
            inputs.remove("sum")
        for a in inputs:
            if (a) == "not":
                inputs.remove(a)
                op1, op2 = inputs[0], inputs[1]
                inputs.remove(op1)
                inputs.remove(op2)
                if op1 != op2:
                    stack.append('0')
                else:
                    stack.append('1') 
                continue  
            if (a) == 'power':
                inputs.remove(a)
                continue       
            if type(a) is float:
                stack.append(a)
                continue 
            op1, op2 = stack.pop(), stack.pop()
            #if a == 'power':
    
            if a == '+':
                stack.append(op2 + op1)
            elif a == '-':
                stack.append(op1 - op2)
            elif a == '*':
                stack.append(op2 * op1)
            elif a == '/':
                stack.append(op1 / op2)
            elif a == '=':
                if op1 != op2:
                    stack.append('0')
                else:
                    stack.append('1')  
    
    
        if (len(stack) > 1):
            lenStack = len(stack)-1
            for x in range(0, lenStack):
                stack.append('+')
            stack.append(_calcSum(stack))
        return stack.pop()
    
    def _calcSum(stack):
        newStack = []
        for a in stack:
            if type(a) is float:
                newStack.append(a)
                continue
            op1, op2 = newStack.pop(), newStack.pop()
            if a == '+':
                newStack.append(op2 + op1)
            elif a == '-':
                newStack.append(op1 - op2)
            elif a == '*':
                newStack.append(op2 * op1)
            elif a == '/':
                newStack.append(op1 / op2)
        return newStack.pop()
    

However I'm having trouble with the NOT and POWER statement; I can't figure
out how to automatically check for these. Could anyone possibly point me in
the right direction or assist with my code? When I try to check for 'power' it
simply skips the rest of my code and attempts to print stack - which is empty
causing an error.


Answer: I think that the following code might be what you want:

    
    
    import math
    
    test_input = ['2', '1', '=', 'not', '2', '3', 'power', '+']
    
    def calculate(input):
        newStack = []
        for a in input:
            print newStack
    
            if a == '+':
                op1, op2 = newStack.pop(), newStack.pop()
                newStack.append(op2 + op1)
            elif a == '-':
                op1, op2 = newStack.pop(), newStack.pop()
                newStack.append(op1 - op2)
            elif a == '*':
                op1, op2 = newStack.pop(), newStack.pop()
                newStack.append(op2 * op1)
            elif a == '/':
                op1, op2 = newStack.pop(), newStack.pop()
                newStack.append(op1 / op2)
            elif a == '=':
                op1, op2 = newStack.pop(), newStack.pop()
                if op1 == op2:
                    newStack.append(1)
                else:
                    newStack.append(0)
            elif a == 'not':
                op = newStack.pop()
                if op > 0:
                    newStack.append(0)
                else:
                    newStack.append(1)
            elif a == 'power':
                op1, op2 = newStack.pop(), newStack.pop()
                newStack.append(math.pow(op1, op2))
            else:
                newStack.append(float(a))
    
        return newStack.pop()
    

As PeterE pointed out in his comment your postfix code is wrong. I assume that
your wanted postfix code probably is `2 1 = not 2 3 power +`. Also note that
the end value then would be `9+1 = 10` and not `8+0 = 8`.

Wikipedia has a nice page on postfix code:
<http://en.wikipedia.org/wiki/Reverse_Polish_notation>.

In the python code one function actually should be enough to push and pop all
the needed stuff onto a stack. To achieve a basic implementation you can then
simply check all your different operator cases and perform any needed
operations. If none of the provided operators matches the current element, you
can assume that the element is a numeric value and simply push the parsed
float value.

Note that this is a very quick and dirty implementation, but it might lead you
on the right track.

