Microsoft Word - Program_01.docx SCSU CSC212‐03 Fall 2019 Programming Assignment 01: Stacks, Queues, and DEqueues Due Date: 10‐07‐2019 (in‐class at 6:15pm) In this programming assignment, you will...

1 answer below »
Are you able to complete this in time?


Microsoft Word - Program_01.docx SCSU CSC212‐03 Fall 2019  Programming Assignment 01:  Stacks, Queues, and DEqueues    Due Date: 10‐07‐2019 (in‐class at 6:15pm) In this programming assignment, you will need to solve the following two problems: Problem#1: You have to design a stack that uses two queues for data storage (instead of, for example, a python list as we’ve seen in class). Specifically, you need to write a class named ‘Stack_2Queues’ that will have the following member methods: push, pop, size, and is_empty. Follow standard OOP principles of Python as discussed in the class. In addition to the stack ADT methods mentioned above, also include (i) a method in your class to convert an object/instance of your class to a string representation, such that that object can be passed to the print() function for viewing its contents (vertically, the top element should be at the top), and (ii) a method that allows an instance of this class to be passed to the len() function and shows its size. Save this class in a file named ‘MyStack.py’. For the two queue instances, use the queue class ‘Queue_PlistLR’ made available to you with this assignment. You also need to write a python program (and save it as MyStack_test.py) to demonstrate that your implementation is correct by comparing it with the results of usual stack operations performed on a reference stack (use the class ‘Stack_PlistLR’ as the reference stack). Hints: For the test program, perform the same operations on the two implementations of a stack and show their contents after each operation. Use a tabular form to display this information. Problem#2: Design a queue that uses two stacks for storing data (instead of, for example, a python list as we’ve seen in class). Specifically, you need to write a class named ‘Queue_2Stacks’ that will have the following member methods: enqueue, dequeue, size, and is_empty. In addition to the stack ADT methods mentioned above, also include (i) a method in your class to convert an object of your class to a string representation, such that the object can be passed to the print() function for viewing its contents (horizontally, the front element should be at the leftmost position), and (ii) a method that allows an instance of this class to be passed to the len() function and shows its size. Save this class in a file named ‘MyQueue.py’. For the two stacks, use the stack class ‘Stack_PlistLR’ made available to you with this assignment. You also need to write a python program (save it as MyQueue_test.py) to demonstrate that your implementation is correct by comparing it with the results of operations performed on a reference queue (use the class ‘Queue_PlistLR’ as the reference queue). Hints: Perform the same operations on the two implementations of a queue and show their contents after each operation. Use a tabular form to display this information. Additional instructions: You CANNOT modify either Stack_PlistLR or Queue_PlistLR. Assume as if they are made available to you by a third party, and you are a python developer who has been tasked to implement a Stack and a Queue class using those, without modifying the supplied classes as other people might be using them in their programs/library development, too. Submission/Demonstration Instructions: This  is  an  individual  assignment  that  is  due  in‐class  on  the  due  date.  Once  you  have  completed  implementing the program, the instructor should be demonstrated of its functionality in order for you to  receive credits for it. Failing to demonstrate will result in a zero credit.
Answered Same DayOct 02, 2021

Answer To: Microsoft Word - Program_01.docx SCSU CSC212‐03 Fall 2019 Programming Assignment 01:...

Yogesh answered on Oct 06 2021
149 Votes
MyQueue_test.py
class Empty(Exception):
"""Error attempting to access an element from an empty container."""
print(Exception)
class Stack_PlistLR:
def __init__(self):
"""Create an empty stack."""
self.data = [] # nonpublic list instance
def size(self):
"""Return the number of ele
ments in the stack."""
return len(self.data)
def isEmpty(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.isEmpty():
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.isEmpty():
raise Empty("Stack is empty")
return self.data.pop() # remove last item from list
def __str__(self):
str_s = ""
for e in self.data:
str_s += str(e) + " "
return str_s.strip()
class Queue_2Stacks(Stack_PlistLR):
def __init__(self):
super().__init__()
self.queue = []
def push(self, stack):
return self.queue.append(stack)
def pop(self):
if len(self.queue) == 0:
print('Queue IS EMPTY.')
else:
return self.queue.pop()
def size(self):
return len(self.queue)
def is_empty(self):
if len(self.queue) == 0:
print('Queue IS EMPTY.')
def obj_to_str(self):
j = 1
for q in self.queue:
print('Stack-' + str(j) + ': ')
for i in q:
itm = str(i)
print(itm)
j = j + 1
def size_of_inst(self):
for s in self.queue:
return len(s)
def menu():
print('\nPlease select an option from below: ')
print('1. Queue size.')
print('2. Print Queue.')
print('3. Delete an item from Stack-1.')
print('4. Delete an item from Stack-2.')
print('5. EXIT.')
ask = input('Enter an Option: ')
if ask == '1':
print(main_queue.size())
menu()
elif ask == '2':
main_queue.obj_to_str()
menu()
elif ask == '3':
element = main_queue.queue[0]
element.pop()
menu()
elif ask == '4':
element2 = main_queue.queue[1]
element2.pop()
menu()
elif ask == '5':
quit()
else:
menu()
def add_element2():
ip = input('Enter an item to insert in the Stack-2 : ')
stack2.push(ip)
print(stack2.__str__())
ask = input('Do you want add another item in Stack-2 (Y/N): ')
if ask.upper() == 'Y':
add_element2()
else:
print('Queue-2 is: ' + stack2.__str__())
print('Size: ' + str(stack2.size()))
main_queue.push(stack2.data)
print(main_queue.data)
print(main_queue.size)
# starting...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here