Python infix to postfix conversion

Discussion in 'Programming' started by Ferris D Morris, Jul 12, 2022.

  1. #1
    In Python, I've begun to solve Data Structure difficulties. I'm converting from infix to postfix, however, I'm having trouble debugging a mistake in my software. The last return statement, when I do the join operation, contains NONE input.
    When I started debugging, I saw that something is incorrect in this section of code following the first push operation (for *). After that, whenever I call pop(), it returns NONE instead of *. Could someone please point out what is wrong here?
        *else:
            while (not s.isEmpty()) and (prec[s.peek()] >= prec[token]):
                #print token
                outlst.append(s.pop())
                #print outlst
            
            s.push(token)
            print (s.peek())*
    Code (markup):
    Source: Scaler
     
    Last edited: Oct 20, 2022
    Ferris D Morris, Jul 12, 2022 IP
  2. salvados guira

    salvados guira Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    my son is very good for you problem, i speak him and he found your solution i send you
     
    salvados guira, Jul 19, 2022 IP
  3. Mark Elijah

    Mark Elijah Greenhorn

    Messages:
    145
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    18
    #3
    The issue likely lies in your indentation. The print(s.peek()) line after s.push(token) might be outside the while loop. This means it would try to print the element on the stack (s.peek()) even if the loop didn't add anything (resulting in NONE if the stack is empty). Make sure print(s.peek()) is indented within the while loop to only print after a value is popped and added to the output list.
     
    Mark Elijah, May 14, 2024 IP