class ArrayStack: """LIFO Stack implementation using a Python list as underlying storage.""" def __init__(self): """Create an empty stack.""" self._data = [] # nonpublic list instance def __len__(self): """Return the number of elements in the stack.""" return len(self._data) def is_empty(self): """Return True if the stack is empty.""" return len(self._data) == 0 def push(self, e): """Add element e to the top of the stack.""" self._data.append(e) # new item stored at end of list def top(self): """Return (but do not remove) the element at the top of the stack. Raise Empty exception if the stack is empty. """ if self.is_empty(): raise Empty('Stack is empty') return self._data[-1] # the last item in the list def pop(self): """Remove and return the element from the top of the stack (i.e., LIFO). Raise Empty exception if the stack is empty. """ if self.is_empty(): raise Empty('Stack is empty') return self._data.pop() # remove last item from list def precedence(opr1, opr2): def convertToPostfix(infx): pfx = '' stack = [] elif sym = "+" or "-" or "/" or "*": for j in range(len(stack)): if stack.top() if __name__ == '__main__': S = ArrayStack() # contents: [ ] S.push(5) # contents: [5] S.push(3) # contents: [5, 3] print(len(S)) # contents: [5, 3]; outputs 2 print(S.pop()) # contents: [5]; outputs 3 print(S.is_empty()) # contents: [5]; outputs False print(S.pop()) # contents: [ ]; outputs 5 print(S.is_empty()) # contents: [ ]; outputs True S.push(7) # contents: [7] S.push(9) # contents: [7, 9] print(S.top()) # contents: [7, 9]; outputs 9 S.push(4) # contents: [7, 9, 4] print(len(S)) # contents: [7, 9, 4]; outputs 3 print(S.pop()) # contents: [7, 9]; outputs 4 S.push(6) # contents: [7, 9, 6] S.push(8) # contents: [7, 9, 6, 8] print(S.pop()) # contents: [7, 9, 6]; outputs 8   I have the instructions attached. I'm not looking for an answer, just a better undertsanding of how to execute this project. I have tried different methods to implement the two functions. I have yet to creat a main class file but I can mange that. I just want to know how I can apprach this code is a more effective way. The pseudo code given in the instructions is quite confusing.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

This is what i have for array stack to far:

from exceptions import Empty

class ArrayStack:
"""LIFO Stack implementation using a Python list as underlying storage."""

def __init__(self):
"""Create an empty stack."""
self._data = [] # nonpublic list instance

def __len__(self):
"""Return the number of elements in the stack."""
return len(self._data)

def is_empty(self):
"""Return True if the stack is empty."""
return len(self._data) == 0

def push(self, e):
"""Add element e to the top of the stack."""
self._data.append(e) # new item stored at end of list

def top(self):
"""Return (but do not remove) the element at the top of the stack.

Raise Empty exception if the stack is empty.
"""
if self.is_empty():
raise Empty('Stack is empty')
return self._data[-1] # the last item in the list

def pop(self):
"""Remove and return the element from the top of the stack (i.e., LIFO).

Raise Empty exception if the stack is empty.
"""
if self.is_empty():
raise Empty('Stack is empty')
return self._data.pop() # remove last item from list

def precedence(opr1, opr2):

def convertToPostfix(infx):
pfx = ''
stack = []
elif sym = "+" or "-" or "/" or "*":
for j in range(len(stack)):
if stack.top()


if __name__ == '__main__':
S = ArrayStack() # contents: [ ]
S.push(5) # contents: [5]
S.push(3) # contents: [5, 3]
print(len(S)) # contents: [5, 3]; outputs 2
print(S.pop()) # contents: [5]; outputs 3
print(S.is_empty()) # contents: [5]; outputs False
print(S.pop()) # contents: [ ]; outputs 5
print(S.is_empty()) # contents: [ ]; outputs True
S.push(7) # contents: [7]
S.push(9) # contents: [7, 9]
print(S.top()) # contents: [7, 9]; outputs 9
S.push(4) # contents: [7, 9, 4]
print(len(S)) # contents: [7, 9, 4]; outputs 3
print(S.pop()) # contents: [7, 9]; outputs 4
S.push(6) # contents: [7, 9, 6]
S.push(8) # contents: [7, 9, 6, 8]
print(S.pop()) # contents: [7, 9, 6]; outputs 8

 

I have the instructions attached. I'm not looking for an answer, just a better undertsanding of how to execute this project. I have tried different methods to implement the two functions. I have yet to creat a main class file but I can mange that. I just want to know how I can apprach this code is a more effective way. The pseudo code given in the instructions is quite confusing.

Design, implement, debug, and test a Python script that converts an infix expression to an equivalent postfix
expression.
Here is the process for converting an infix expression to postfix:
1. Initialize the postfix string (pfx) to the empty string and create a new stack (st)
2. Take the next symbol, sym, from the infix string(infx)
o If sym is an operand, append sym to pfx
o If sym is (, push sym onto the stack
o If sym is ), pop and append all the symbols from the stack until the most recent left parenthesis. Pop and
discard the left parenthesis.
o If sym is an operator:
Pop and append all the operators from the stack to pfx that are above the most recent left parenthesis and
have precedence greater than or equal to sym
· Push sym onto the stack
3. After processing infx, some operators might be left on the stack. Pop and append to pfx everything from the stack.
The valid operators are: +,
,1. The operands will be single letter variables such as a, b, c. An example of a valid infix
expression for this program is:
(a+b)/c
which is converted to postfix as: a b + c /
Your Python program should import class ArrayStack (array_stack.py
and implement two functions:
convertToPostfix(infx) which converts the infix expression to postfix
precedence(opr1, opr2) which determines the precedence between two operators. If the first operator is of higher or
equal precedence than the second operator, it should return True; otherwise it returns False.
Transcribed Image Text:Design, implement, debug, and test a Python script that converts an infix expression to an equivalent postfix expression. Here is the process for converting an infix expression to postfix: 1. Initialize the postfix string (pfx) to the empty string and create a new stack (st) 2. Take the next symbol, sym, from the infix string(infx) o If sym is an operand, append sym to pfx o If sym is (, push sym onto the stack o If sym is ), pop and append all the symbols from the stack until the most recent left parenthesis. Pop and discard the left parenthesis. o If sym is an operator: Pop and append all the operators from the stack to pfx that are above the most recent left parenthesis and have precedence greater than or equal to sym · Push sym onto the stack 3. After processing infx, some operators might be left on the stack. Pop and append to pfx everything from the stack. The valid operators are: +, ,1. The operands will be single letter variables such as a, b, c. An example of a valid infix expression for this program is: (a+b)/c which is converted to postfix as: a b + c / Your Python program should import class ArrayStack (array_stack.py and implement two functions: convertToPostfix(infx) which converts the infix expression to postfix precedence(opr1, opr2) which determines the precedence between two operators. If the first operator is of higher or equal precedence than the second operator, it should return True; otherwise it returns False.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education