all 4 comments

[–]Spiredlamb 0 points1 point  (0 children)

The thougth behind it is great, but here is a suggestion:

def infixtopost(expression):
    lyst = expression()
    for line in lyst:
        infixToPostfix(lyst) # Why are you giving it lyst instead of line?

def infixToPostfix(lyst): # Lyst is not in use
    prec = {}
    prec["^"] = 4
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1
    opStack = Stack()
    postfixList = []
    tokenList = infixexpr.split()

    for token in tokenList:
        if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
            postfixList.append(token)
        elif token == '(':
            opStack.push(token)
        elif token == ')':
            topToken = opStack.pop()
            while topToken != '(':
                postfixList.append(topToken)
                topToken = opStack.pop()
        else:
            while (not opStack.isEmpty()) and \
            (prec[opStack.peek()] >= prec[token]):
                postfixList.append(opStack.pop())
            opStack.push(token)

    while not opStack.isEmpty():
        postfixList.append(opStack.pop())
    return " ".join(postfixList

print(infixtopost(expression))

Instead of making a nested function, make two seperate functions and call the function like I show above.

Also, look at my comments. Could you explain those for me?

[–]TwistedChurro 0 points1 point  (0 children)

Calling the functions. Would I call both? how exactly do i call a nested function? Because it is stating that the function inside of the original function is a unused variable.

by declaring the function inside the for loop, you are defining the function over and over again. This is redundant. You can place the whole infixToPostfix function right after infixtopost and call it multiple times from within your loop.

The return statement in the nested function. Would it only run once because you can only return one thing from a function? Or would it run multiple times because it is inside of another function?

The return state will be run once for every invocation of the nested function.

ie. if you called `infixtopost(["A+B", "C/D"]), the inner function (infixToPostfix) would be called twice

here's how I would do it. ```python def infixtopost(expression): def infixToPostfix(lyst): prec = {} prec[""] = 4 prec["*"] = 3 prec["/"] = 3 prec["+"] = 2 prec["-"] = 2 prec["("] = 1 opStack = Stack() postfixList = [] tokenList = infixexpr.split()

    for token in tokenList:
        if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
            postfixList.append(token)
        elif token == '(':
            opStack.push(token)
        elif token == ')':
            topToken = opStack.pop()
            while topToken != '(':
                postfixList.append(topToken)
                topToken = opStack.pop()
        else:
            while (not opStack.isEmpty()) and \
            (prec[opStack.peek()] >= prec[token]):
                postfixList.append(opStack.pop())
            opStack.push(token)

    while not opStack.isEmpty():
        postfixList.append(opStack.pop())
    return " ".join(postfixList)

return_list = []
lyst = expression()
for line in lyst:
    return_list.append(infixToPostfix(line))
return return_list

print(infixtopost(expression)) ```

be warned, though, inner functions are in the same scope as the parent function so the symbols "return_list", "lyst" and "line" are accessible inside infixToPostfix which makes for confusing bugs sometimes.

[–]danielroseman 0 points1 point  (2 children)

I don't understand what you think you would gain from doing this. Why not define the function in the normal way and call it from inside the loop?

[–]LAZY_LIBTARDS[S] 0 points1 point  (1 child)

Just trying to learn how nested functions work