2025-10-13
Testing Centerpractice questions are posted on CanvasCreate a new stack
Locate the character 'P' in the grid
Push its location onto the stack
While the stack is not empty
Pop a location, (row, column), off the stack
If the grid contains 'T' at this location, then
A path has been found
Return True
Else if this location does not contain a dot
Place a dot in the grid at this location
Examine the adjacent cells to this one and
for each one that contains a space,
push its location onto the stack
Return False
Python Virtual Machine (PVM) then executes thesebasePtr’s current value in the region labeled Prev basePtr and sets the basePtr to the new activation record’s baselocationCounter’s current value in the region labeled Return Address and sets the locationCounter to the first instruction of the called subroutineParameterslocationCounterlocationCounter and the basePtr from values stored in the activation recordlocationCounterArrayStack and LinkedStacktester program that shows how you can test them immediately
ArrayStack, LinkedStack) inherit from the base class and provide specific implementations.ArraySortedBag class, a subclass of the ArrayBag class:
ArrayBag is called the parent or superclass of ArraySortedBagArrayBag class implements BagInterface, the ArraySortedBag class also implements this interface via inheritanceThis diagram depicts a subclass and inheritance in a class diagram
_init_ method in the new class_init_ method in ArraySortedBag must call the _init_ method in the parent class ArrayBag
ArrayBag._init_(self, sourceCollection)_init_ method to run_init_ methodfrom arraybag import ArrayBag
class ArraySortedBag(ArrayBag):
"""An array-based sorted bag implementation."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it’s present."""
ArrayBag.__init__(self, sourceCollection)ArrayStack and LinkedStack
AbstractStack class, which in turn is a subclass of AbstractCollectionAbstractStack class, the size variable, and the methods isEmpty, __len__, __str__, __add__, and __eq__ from AbstractCollectionArrayStack and LinkedStack are _init_, peek, push, pop, clear, and _iter_The stack resources in the collection hierarchy

class AbstractStack(object):
"""Abstract class that implements the common
methods for stack collections"""
# Constructor
def __init__(self, sourceCollection=None):
self.size = 0
if sourceCollection:
for i in sourceCollection:
self.push(i)
def isEmpty(self):
"""Returns True if stack is empty. Otherwise, returns False
"""
return len(self) == 0
def __len__(self):
"""Returns the number of items in stack
"""
return self.size
def __str__(self):
"""Returns the string repsentative of S
"""
return "{" + ", ".join(map(str,self)) +"}"from arrays import Array
from abstractstack import AbstractStack
class ArrayStack(AbstractStack):
"Array-based stack collections"
# Array inital capacity
DEFAULT_CAPACITY = 10
#Constructor
"""Initializes the state of self which include any items from sourcecollector"""
def __init__(self, sourceCollection = None):
self.items = Array(ArrayStack.DEFAULT_CAPACITY)
AbstractStack.__init__(self, sourceCollection)
# Accessors
def __iter__(self):
"""Supports iteration over self. Visits items bottom to top of stack"""
cursor =0
while cursor < len(self):
yield self.items[cursor]
cursor +=1
def peek(self):
if self.isEmpty():
raise KeyError("The stack is empty")
return self.items[len(self)-1]
# Mutator functions
def push(self, item):
#resize the array here if neccesary
self.items[len(self)] = item
self.size +=1
def clear(self):
self.size =0
self.items = Array(ArrayStack.DEFAULT_CAPACITY)
def pop(self):
#Check the precondition that the stack is not empty
if self.isEmpty():
raise KeyError("The stack is empty")
oldItem = self.items[len(self)-1]
self.size -=1
# resize the Array here if necessary
return oldItem"""
A tester program for stack implementations.
"""
from arraystack import ArrayStack
#from linkedstack import LinkedStack
def test(stackType):
# Test any implementation with the same code
s = stackType()
print("Length:", len(s))
print("Empty:", s.isEmpty())
print("Push 1-12")
for i in range(12):
s.push(i + 1)
print("Peeking:", s.peek())
print("Items (bottom to top):", s)
print("Length:", len(s))
print("Empty:", s.isEmpty())
theClone = stackType(s)
print("Items in clone (bottom to top):", theClone)
theClone.clear()
print("Length of clone after clear:", len(theClone))
print("Push 11")
s.push(11)
print("Popping items (top to bottom):", end = " ")
while not s.isEmpty(): print(s.pop(), end = " ")
print("\nLength:", len(s))
print("Empty:", s.isEmpty())
test(ArrayStack)
#test(LinkedStack)LinkedStack and Node