TORONTO KIDS COMPUTER CLUB | Friday 17:00 Python Practice 21.01.22.
18973
post-template-default,single,single-post,postid-18973,single-format-standard,ajax_fade,page_not_loaded,,qode-theme-ver-7.6.2,wpb-js-composer js-comp-ver-6.10.0,vc_responsive

Friday 17:00 Python Practice 21.01.22.

24 Jan Friday 17:00 Python Practice 21.01.22.

Question:
Assume the postfix expression is a string of characters separated by spaces. The operators are *, /, +, and – and the operands are assumed to be single-digit integer values. The output will be an integer result. The following are the steps how to calculate the value of the postfix expression:

    1. Create an empty stack called s.
    2. Convert the string to a list by using the string method split.
    3. Scan the token list from left to right.
      • If the token is an operand, convert it from a string to an integer and push the value onto the s.
      • If the token is an operator, *, /, +, or -, it will need two operands. Pop the s twice. The first pop is the second operand and the second pop is the first operand. Perform the arithmetic operation. Push the result back on the s.
    4. When the input expression has been completely processed, the result is on the stack. Pop the s and return the value.

Please create a function for calculating the value of the given postfix expressions.

 

No Comments

Sorry, the comment form is closed at this time.