Page 1 of 1

Fill in the blanks with the Python code. What goes in the "" sections for Parts 1-13. Dependent files screens

Posted: Fri Apr 29, 2022 7:00 am
by answerhappygod
Fill in the blanks with the Python code. What goes in
the "<your code>" sections for
Parts 1-13. Dependent files screenshotted.
File: useStack.py
The following files must be in the same folder:
abstractcollection.py
abstractstack.py
arraystack.py
arrays.py
linkedstack.py
node.py
"""
#
# Replace with your name.
# Replace any "" comments with your own code statement(s)
# to accomplish the specified task.
# DO NOT CHANGE ANY OTHER CODE.
from arraystack import ArrayStack
from linkedstack import LinkedStack
def printStacks():
print("stack1:", stack1)
print("stack2:", stack2)
print("stack3:", stack3)
print()
# Here are the starting stacks:
stack1 = ArrayStack([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
stack2 = ArrayStack([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
stack3 = LinkedStack() # empty
# Print the stacks:
printStacks()
# Part 1:
# Use the == comparison operator to determine if stack1 and stack2
are equal.
# If they are equal print "stack1 and stack2 are equal.".
# If they are not equal print "stack1 and stack2 are not
equal."
#
print()
# Part 2:
# Move each item from stack2 to stack3.
#
print("After moving each item from stack2 to stack3:")
printStacks()
# Part 3:
# Use the == comparison operator to determine if stack1 and stack3
are equal.
# If they are equal print "stack1 and stack3 are equal.".
# If they are not equal print "stack1 and stack3 are not
equal."
#
print()
# Part 4:
# Remove the top item from stack1 and print the removed item:
#
print("After removing the top item from stack1:")
printStacks()
# Part 5:
# Remove items from stack1 until it has only 2 items
remaining:
#
print("After removing all but 2 items from stack1:")
printStacks()
# Part 6:
# Use a single method to empty stack1:
#
print("After emptying stack1:")
printStacks()
# Part 7:
# Move all even valued items from stack3 to stack1.
# This will leave stack3 empty.
#
print("After moving evens from stack3 to stack1:")
printStacks()
# Part 8:
# Move all the stack1 items back to stack3.
# This will leave stack1 empty.
#
print("After moving evens back to stack3:")
printStacks()
# Part 9:
# Get and print the value at the top of stack3 without removing
it:
#
print("The value at the top of stack3:", item)
printStacks()
# Part 10:
# Use isEmpty() to check whether stack1 and stack3 are empty.
# If either is empty, print a message saying it is empty.
# If either is not empty, print a message saying it is not
empty.
#
print()
# Part 11:
# Add the odd single-digit numbers to stack1 with 9 at the
top:
#
print("After adding the odd single-digit numbers to stack1:")
printStacks()
# Part 12:
# Alternate removing items from stack3 and stack1, interleaving
them onto stack2.
# Both stacks 1 and 3 will be left empty.
#
print("After moving items from stack3 and stack1 (interleaved) to
stack2:")
printStacks()
# Part 13:
# Move each item from stack2 to both stack1 and stack3.
# stack2 will be left empty.
#
print("After moving each item from stack2 to stacks 1 and
3:")
printStacks()
# Part 14:
# Move each item from stack3 to stack2, preserving their
order.
# To facilitate this special move, use a new stack called
viaStack.
# stack3 will be left empty.
#
print("After moving each item from stack3 to stack2, preserving
their order:")
printStacks()
Fill In The Blanks With The Python Code What Goes In The Your Code Sections For Parts 1 13 Dependent Files Screens 1
Fill In The Blanks With The Python Code What Goes In The Your Code Sections For Parts 1 13 Dependent Files Screens 1 (221.85 KiB) Viewed 28 times
File: abstractcollection.py Author: Ken Lambert, copyright 2015, 2020 Used by permission. class AbstractCollection(object) An abstract collection implementation. # Constructor def __init__(self, source Collection = None): Sets the initial state of self, which includes the contents of sourceCollection, if it's present. self.size = 0 self.modCount = 0 if sourceCollection: for item in sourceCollection: self.add(item) # Accessor methods def_add__(self, other): ***Returns a new collection containing the contents of self and other." result-type(self) (self) for item in other: result.add(item) return result def _ed_(self, other): Returns True if self equals other, or False otherwise. Compares pairs of items in the two sequences generated by the iterators on self and other. if self is other: return True if type(self).INTERFACE != type(other).INTERFACE or len(self) != len(other): return False otheriter = iter(other) for item in self: if item != next(otherlter): return false return True def _len_(self): Returns the number of items in self. return self size def _str_(self): "Returns the string representation of self, using [] as delimiters. return "L" +","join(map(str, self)) = ")" def count(self, item): Returns the number of instance of item in self. counter = 0 for nextitem in self: if item == nextltem: counter += 1 return counter def isEmpty(self): "Returns True if len(self) == 0, or False otherwise.-** return len(self) == 0 # These methods track and update the modCount, which is used to # prevent mutations within the context of an iterator (for loop) def getModCount(self): Returns the number of modifications to the collection. return self.modCount def incModCount(self): Increments the number of modifications to the collection. self.modCount += 1 File: abstractstack.py Author: Ken Lambert, copyright 2015, 2020 Used by permission. from abstractcollection import AbstractCollection class AbstractStack(AbstractCollection): **Represents an abstract stack." # Constructor def __init__(self, sourceCollection) Initializes the stack at this level.“ AbstractCollection _init__(self, sourceCollection) # Mutator methods def add(self, item): For compatibility with other collections. self.push(item) File: arrays.py Author: Ken Lambert, copyright 2015, 2020 Used by permission. An Array is like a list, but the client can use only (), len, iter, and str. To instantiate, use <variable> = Array(<capacity>, <optional fill value>) The fill value is None by default. class Array(object) Represents an array- # Constructor def _init__(self, capacity, fillValue = None): **Capacity is the static size of the array. fillValue is placed at each position. self.items = listo for count in range(capacity): self.items.append(fillValue) # Accessor methods def_iter_(self): Supports iteration over a view of an array return iter(self.items) def _getitem_(self, index): Subscript operator for access at index. return self.items[index] def_len_(self): -> The capacity of the array return len(self.items) def _str_(self): -> The string representation of the array.*** return str(self.items) # Mutator methods def _setitem_(self, index, newitem): Subscript operator for replacement at index. self.items[index] = newltem File: linkedstack.py Author: Ken Lambert, copyright 2015, 2020 Used by permission. from abstractstack import AbstractStack from node import Node class LinkedStack(AbstractStack): ***Represents a link-based stack.*** # Class variables INTERFACE = "Stackinterface" # Constructor def _init__(self, sourceCollection = None): self.head = None AbstractStack__init_(self, sourceCollection) # Accessor methods def _iter_(self): Supports iteration over a view of self. Visits items from bottom to top of stack. lyst = listo probe = self.head while probe != None: lyst.append(probe.data) probe = probe.next lyst.reverse modCount = self.getModCount cursor = 0 while cursor < len(lyst): yield lyst[cursor] if modCount != self.getModCount(): raise AttributeError("Illegal modification of backing store") cursor == 1 def peek(self): "Returns the item at the top of the stack. Precondition: the stack is not empty. Raises: KeyError if stack is empty." if self.isEmptyo: raise KeyError("Attempt to peek at empty stack) return self.head.data # Mutator methods def clear(self): *Makes self become empty. self size = 0 self.modCount = 0 self.head = None def pop(self) Removes and returns the item at the top of the stack. Precondition: the stack is not empty. Raises: KeyError if stack is empty. Postcondition: the top item is removed from the stack. if self.isEmpty: raise KeyError("The stack is empty self.size -= 1 self.inc ModCounto data = self. head.data self.head = self.head.next return data def push(self, item): Sets the initial state of self, which includes the contents of sourceCollection, if it's present. self.head-Node(item, self head) self size + 1 self.inc ModCount File: node.py Author: Ken Lambert, copyright 2015, 2020 Used by permission. class Node(object) "Represents a singly linked node. # Constructor def _init__(self, data, next = None): Instantiates a Node with default next of None. self.data-data self.next next class TwoWayNode(Node): "Represents a doubly linked node. # Constructor def __init__(self, data = None, previous = None, next = None): ***Instantiates a TwoWayNode. Node._init__(self, data, next) self.previous - previous