Question
# Constants OPERATORS = +-*/ def postfix(string): ------------------------------------------------------- Evaluates a postfix expression. Use: answer = postfix(string) ------------------------------------------------------- Parameters: string - the postfix string to
# Constants OPERATORS = "+-*/" def postfix(string): """ ------------------------------------------------------- Evaluates a postfix expression. Use: answer = postfix(string) ------------------------------------------------------- Parameters: string - the postfix string to evaluate (str) Returns: answer - the result of evaluating string (float) ------------------------------------------------------- """
The function must use a single stack to do its work.
In a postfix expression, operators follow operands. Thus the infix expression:
12 - 5
is written as postfix expression:
12 5 -
which evaluates to 7.
In postfix expressions as the operations are performed in the order that they appear. The expression:
4 5 + 12 * 2 3 * -
is equivalent to (4 + 5) * 12 - 2 * 3 and evaluates to 102.
The algorithm for evaluation postfix expressions is:
- Create a stack to store operands (i.e. values)
- Walk through the expression string element by element:
- If the element is a number, push it into the stack
- If the element is a operator, pop operands for the operator from stack. Evaluate the operator with the top element on the right of the operator and the next element on the left of the operator, and push the result back to the stack
- When the expression is finished, the number on the top of the stack is the final answer. The stack should be empty after this number is popped.
The Stack Class:
class Stack:
def __init__(self): """ ------------------------------------------------------- Initializes an is_empty stack. Data is stored in a Python list. Use: s = Stack() ------------------------------------------------------- Returns: a new Stack object (Stack) ------------------------------------------------------- """ self._values = []
def is_empty(self): """ ------------------------------------------------------- Determines if the stack is empty. Use: b = s.is_empty() ------------------------------------------------------- Returns: True if the stack is empty, False otherwise ------------------------------------------------------- """ if len(self._values) >0: a = False else: a = True return a
def push(self, value): """ ------------------------------------------------------- Pushes a copy of value onto the top of the stack. Use: s.push(value) ------------------------------------------------------- Parameters: value - a data element (?) Returns: None ------------------------------------------------------- """
self._values.append(deepcopy(value))# Your code here return def pop(self): """ ------------------------------------------------------- Pops and returns the top of stack. The value is removed from the stack. Attempting to pop from an empty stack throws an exception. Use: value = s.pop() ------------------------------------------------------- Returns: value - the value at the top of the stack (?) ------------------------------------------------------- """ assert len(self._values) > 0, "Cannot pop from an empty stack" value = self._values.pop() return value # Your code here
def peek(self): """ ------------------------------------------------------- Returns a copy of the value at the top of the stack. Attempting to peek at an empty stack throws an exception. Use: value = s.peek() ------------------------------------------------------- Returns: value - a copy of the value at the top of the stack (?) ------------------------------------------------------- """ assert len(self._values) > 0, "Cannot peek at an empty stack" value = deepcopy(self._values[-1])
return value # Your code here
def __iter__(self): """ FOR TESTING ONLY ------------------------------------------------------- Generates a Python iterator. Iterates through the stack from top to bottom. Use: for v in s: ------------------------------------------------------- Returns: value - the next value in the stack (?) ------------------------------------------------------- """ for value in self._values[::-1]: yield value
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started