1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Rep Power

    Need help with assignment

    Program #4: Write a Python program that uses a stack to evaluate an RPN expression.
    The user of the program will enter the tokens in an RPN expression one at a time. When the user hits RETURN with no token, the final value of the expression is popped off the stack and displayed.
    A run of the program might look like the following (assuming the user is entering the RPN expression: 3 5 7 + * ):
    Enter RPN expression one token at a time; type RETURN to end expression.
    Type next token: 3
    Type next token: 5
    Type next token: 7
    Type next token: +
    Type next token: *
    Type next token:
    Expression = 36
    Your program should handle all four mathematical operations:
     Addition.
     Subtraction.
     Multiplication.
     Division.
    Remember that subtraction and division are not commutative operations, so you need to take note of the order in which the two numbers are popped off the stack.
    Because you need to store both numbers and operators (characters) on the stack, the data on the stack will be strings. Therefore, when numbers are popped off the stack to be combined (added, subtracted, etc.), they will need to be converted from string to numeric.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    Confused: the stack can be just numbers. Operators make their evaluation consuming the top two values on the stack then push the result onto the stack. You could build the program to load the entire stack, then separately evaluate it which seems to be the recommended course. I see disadvantages more so than advantages with this approach.

    Well dude or dudette, show some code.

    Can you prompt and receive input from the user?
    Can you associate operations with a string?
    Can you use a list as a stack?
    Can you start python?
    Can you write a python script?
    Can you run that python script?

    >>> help([])
    reveals append and pop methods. Try it sometime.

    Affirmatively answering and implementing some of these features will get you part way. And where you answer "no" will give us a specific detail of concern.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo