Question
need helping solving methods Implement a Stack ADT class by completing the provided skeleton code in the file stack_da.py. You will use the Dynamic Array
need helping solving methods
Implement a Stack ADT class by completing the provided skeleton code in the file stack_da.py. You will use the Dynamic Array data structure that you implemented in Assignment 2 as the underlying storage for your Stack ADT. 2. Your Stack ADT implementation will include the following standard Stack methods: push() pop() top() 3. We will test your implementation with different types of objects, not just integers. We guarantee that all such objects will have correct implementations of methods __eq__(), __lt__(), __gt__(), __ge__(), __le__(), and __str__(). 4. The number of objects stored in the Stack at any given time will be between 0 and 1,000,000 inclusive. The stack must allow for the storage of duplicate objects. 5. RESTRICTIONS: You are not allowed to use ANY built-in Python data structures and/or their methods. You must solve this portion of the assignment by importing the DynamicArray class that you wrote in Assignment 2, and using class methods to write your solution. You are also not allowed to directly access any variables of the DynamicArray class (self._da._size, self._da._capacity, and self._da._data). All work must be done by only using class methods
#skelton code
from dynamic_array import * class StackException(Exception): """ Custom exception to be used by Stack class DO NOT CHANGE THIS METHOD IN ANY WAY """ pass class Stack: def __init__(self): """ Init new stack based on Dynamic Array DO NOT CHANGE THIS METHOD IN ANY WAY """ self._da = DynamicArray() def __str__(self) -> str: """ Return content of stack in human-readable form DO NOT CHANGE THIS METHOD IN ANY WAY """ out = "STACK: " + str(self._da.length()) + " elements. [" out += ', '.join([str(self._da[i]) for i in range(self._da.length())]) return out + ']' def is_empty(self) -> bool: """ Return True is the stack is empty, False otherwise DO NOT CHANGE THIS METHOD IN ANY WAY """ return self._da.is_empty() def size(self) -> int: """ Return number of elements currently in the stack DO NOT CHANGE THIS METHOD IN ANY WAY """ return self._da.length() # ----------------------------------------------------------------------- def push(self, value: object) -> None: """ TODO: Write this implementation """ pass def pop(self) -> object:
""" TODO: Write this implementation """ pass def top(self) -> object: """ TODO: Write this implementation """ pass # ------------------- BASIC TESTING ----------------------------------------- if __name__ == "__main__": print(" # push example 1") s = Stack() print(s) for value in [1, 2, 3, 4, 5]: s.push(value) print(s) print(" # pop example 1") s = Stack() try: print(s.pop()) except Exception as e: print("Exception:", type(e)) for value in [1, 2, 3, 4, 5]: s.push(value) for i in range(6): try: print(s.pop()) except Exception as e: print("Exception:", type(e)) print(" # top example 1") s = Stack() try: s.top() except Exception as e: print("No elements in stack", type(e)) s.push(10) s.push(20) print(s) print(s.top()) print(s.top()) print(s)
#examples
push
Example #1: s = Stack() print(s) for value in [1, 2, 3, 4, 5]: s.push(value) print(s)
Output: STACK: 0 elements. [] STACK: 5 elements. [1, 2, 3, 4, 5]
pop
Example #1: s = Stack() try: print(s.pop()) except Exception as e: print("Exception:", type(e)) for value in [1, 2, 3, 4, 5]: s.push(value) for i in range(6): try: print(s.pop()) except Exception as e: print("Exception:", type(e))
Output: Exception: 5 4 3 2 1 Exception:
top
Example #1: s = Stack() try: s.top() except Exception as e: print("No elements in stack", type(e)) s.push(10) s.push(20) print(s) print(s.top()) print(s.top()) print(s)
Output: No elements in stack STACK: 2 elements. [10, 20] 20 20 STACK: 2 elements. [10, 20]
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