REQUIREMENTS: Using Python, you will write a program called array-stack.py that
ID: 3745661 • Letter: R
Question
REQUIREMENTS: Using Python, you will write a program called array-stack.py that implements the stack data structure using an array. Be sure to include the following stack operations in your Python program: CreateStack() creates an empty stack. Push(S, i) pushes item i on top of the stack S. Pop(S) pops the item that is on the top of the stack S. The operation returns the item. If the stack is empty, then the operation is not allowed (i.e., we get an error). Top(S) we get the value of the item on the top of the stack S without removing it. The stack remains the same. If the stack is empty, then the operation again is not allowed and we get an error. IsStackEmpty(S) returns TRUE if stack S is empty, or FALSE otherwise. IMPLEMENTATION DETAILS: Be sure to include your name, along with the Certificate of Authenticity, as comments at the very beginning of your Python code. Also, if you collaborated with others, be sure to state their names as well. Your program should create a single empty stack. That is, your program should not create more than one stack. Your program should display the following menu (looping continuously until the user selects option E): Enter the letter that corresponds to the desired stack operation: A. Push a number (i.e., integer) on the top of the stack. B. Pop a number (i.e., integer) from the top of the stack. C. Display the value of the number (i.e., integer) on the top of the stack. D. Indicate whether the stack is EMPTY or NOT EMPTY. E. End program. **Note: Your menu logic should accept options A, B, C, D, E (uppercase or lowercase). Anything other than these options should generate the following error message to the user: Invalid option! Please enter A, B, C, D or E.
Explanation / Answer
Explanation:
Code in python:
def CreateStack():
S=[]
return S
def Push(S,i):
S.append(i)
def Pop(S):
if IsStackEmpty(S):
print("Stack is Empty")
return -1
else:
val=S[len(S)-1]
S.remove(S[len(S)-1])
return val
def Top(S):
if IsStackEmpty(S):
print("Stack is Empty")
return -1
else:
val=S[len(S)-1]
return val
def IsStackEmpty(S):
return len(S) is 0
def main():
S=CreateStack()
while True:
print("Enter the letter that corresponds to desired stack operation:")
print("A.Push a number(i.e.,integer) on the top of the stack.")
print("B.Pop a number(i.e.,integer) from the top of the stack.")
print("C.Display the value of the number(i.e.,integer) on the top of the stack.")
print("D.Indicate whether the stack is EMPTY or NOT EMPTY.")
print("E. End program")
option = str(input("Your option ::"))
option=option.lower()
if len(option) is 1:
if option == "a":
i = int(input("Enter the value to push :"))
Push(S,i)
elif option == "b":
val=Pop(S)
if val != -1:
print("Value popped is :: ",val)
elif option == "c":
val=Top(S)
if val != -1:
print("Value at Top is :: ",val)
elif option == "d":
if IsStackEmpty(S):
print("Yes!!! Stack is Empty")
else:
print("Nooo!!! Stack is not Empty")
elif option == "e":
break
else:
print("Invalid option! Please enter A,B,C,D or E.")
else:
print("Invalid option! Please enter A,B,C,D or E.")
print(" ")
main()
Thank you!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.