1

Little brief about the code: I have to do a class that will evaluate prefix, postfix or infix expression. It has to determine whether it is pre/post/infix and convert them to postfix, for example prefixTOpostfix() (others are deleted as not needed now) method in the code which converts from '/x7' to 'x7/', the expression is edited in method edit() from 'x7/' to 'x 7 /'. Both methods work fine, tested on multiple examples (not posting here the whole code as it is and assignment, but also they are not needed. The question being asked is just an error I am getting, don't worry I am not asking for solution to my assignment). There is also assign method, because there can be variables, for example 'a = 3', and it can be somewhere in the expression. Problem: When i run print(v.evaluate('x 7/')) (what is already in postfix), where x = 14, it returns 2 as it should. However when I run print(v.evaluate('/x 7')) (what is in prefix) it returns None. Both expressions look exactly the same after they ran through their methods 'x 7 /' (I had testing prints in the code), Both stacks are the same. First there is '14', then '14 7', and lastly '2'. When I change return(s.pop()) to return(s.top()), both expressions are evaluated fine as '2'. So why return(s.pop()) doesn't work with the second one ? If there are more questions about the code or something isn't clear enough, tell me I will try to explain it differently.

class MyClass:

    class Stack:
        ...

    def __init__(self):
        self.table = {}

    def __repr__(self):
        ...

    def assign(self, variable, exp):

        if '+-*/%' in exp: # temporary solution
            exp = self.evaluate(exp)

        self.table[variable] = exp

    def evaluate(self, exp):

        if exp[0] in '+-*/%': # Prefix
            new_exp = self.prefixTOpostfix(exp)
            self.evaluate(new_exp)

        elif exp[len(exp)-1] in '+-*/%': # Postfix
            s = self.Stack()
            exp = self.edit(exp) # from 'x7/' to 'x 7 /'

            for item in exp.split():

                if item == '+':
                    s.push(s.pop() + s.pop())

                ... # other operations

                elif prvok == '/':
                    temp = s.pop()
                    if temp == 0:
                        return None
                    s.push(s.pop() // temp) # it has to be // !

                else: # if it is number / variable
                    if item in self.table:
                        s.push(int(self.table[item]))
                    else:
                        s.push(int(item))

                s.printOUT()

            return(s.pop())

        else: # Infix
            ...


    def prefixTOpostfix(self, exp):

        ...

    def edit(self, exp):

        ...

1 Answer 1

1
    if exp[0] in '+-*/%': # Prefix
        new_exp = self.prefixTOpostfix(exp)
        self.evaluate(new_exp)

You need to return the result of your recursive call.

    if exp[0] in '+-*/%': # Prefix
        new_exp = self.prefixTOpostfix(exp)
        return self.evaluate(new_exp)
Sign up to request clarification or add additional context in comments.

2 Comments

Oh my god, you are right ! I did not realise it until now. What a stupid mistake. If I can ask, I understand (or at least semi - understand), why pop() didn't work, but why did top() work ?
Ah, well I can't do that, but still Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.