# This program exercises stacks. # Replace any \"yourcode\" comments with your o
ID: 3712157 • Letter: #
Question
# This program exercises stacks.
# Replace any "yourcode" comments with your own code statement(s)
# to accomplish the specified task.
# Do not change any other code.
# The following files must be in the same folder:
# abstractcollection.py
# abstractstack.py
# arraystack.py
# arrays.py
# linkedstack.py
# node.py
from arraystack import ArrayStack
from linkedstack import LinkedStack
def printStack1():
print("stack1:", stack1)
print()
def printStack2():
print("stack2:", stack2)
print()
def print2Stacks():
print("stack1:", stack1)
print("stack2:", stack2)
print()
def print3Stacks():
print("stack1:", stack1)
print("stack2:", stack2)
print("stack3:", stack3)
print()
# Here are 2 starting stacks:
stack1 = ArrayStack([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
stack2 = ArrayStack([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# Print the stacks:
print2Stacks()
# Part 1:
# Use the == comparison operator to determine if the 2 stacks are equal.
# If they are equal print "The stacks are equal.".
# If they are not equal print "The stacks are not equal."
#yourcode
print()
# Part 2:
# Remove the top item from stack1, print the removed item, and print stack1:
#yourcode
print("After removing the top item from stack1:")
printStack1()
# Part 3:
# Use the == comparison operator to determine if the 2 stacks are equal.
# If they are equal print "The stacks are equal.".
# If they are not equal print "The stacks are not equal."
#yourcode
print()
# Part 4:
# Remove all the items from stack1 until there is only 3 items left in it:
#yourcode
print("After removing all but 3 items from stack1:")
printStack1()
# Part 5:
# Use a single method to empty stack1:
#yourcode
print("After emptying stack1:")
printStack1()
# Part 6:
# Use pop() and push() to move all even valued items from stack2 to stack1.
# This will leave stack2 empty.
# This will leave stack1 with only even valued items.
# stack1 will be in the reverse order from the original stack2 order.
# When popping, use a try/except block to catch and ignore the KeyError exception.
#yourcode
print("After moving evens to stack1 (in reverse order):")
print2Stacks()
# Part 7:
# Use pop() and push() to move all the stack1 items back to stack2.
# This will leave stack1 empty.
# This will leave stack2 with the even valued items back in their original order.
# You have effectively removed all the odd valued items from stack2.
# You will again need a try/except block.
#yourcode
print("After moving evens back to stack2 (in original order):")
print2Stacks()
# Part 8:
# Get and print the value at the top of stack2 without removing it:
#yourcode
print("The value at the top of stack2:", item)
printStack2()
# Part 9:
# Use isEmpty() to check whether stack1 and stack2 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.
#yourcode
print()
# Part 10:
# Add the odd single-digit numbers to stack1 with 9 at the top:
#yourcode
print("After adding the odd single-digit numbers to stack1:")
print2Stacks()
# Part 11:
# Create a new empty stack of type LinkedStack called stack3:
stack3 = LinkedStack()
# Alternate popping items from stack2 and stack1, interleaving them onto stack3.
# Both stacks 1 and 2 will be left empty.
# As usual, handle or avoid exceptions.
#yourcode
print("After moving items from stack1 and stack2 (interleaved) to stack3:")
print3Stacks()
# Part 12:
# Move each item from stack3 to both stack1 and stack2.
# Stacks 1 and 2 will be left in their original starting order.
# stack3 will be left empty.
#yourcode
print("After moving each item from stack3 to stacks 1 and 2:")
print3Stacks()
******Output****
stack1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
stack2: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The stacks are equal.
Removed item: 10
After removing the top item from stack1:
stack1: [1, 2, 3, 4, 5, 6, 7, 8, 9]
The stacks are not equal.
After removing all but 3 items from stack1:
stack1: [1, 2, 3]
After emptying stack1:
stack1: []
After moving evens to stack1 (in reverse order):
stack1: [10, 8, 6, 4, 2]
stack2: []
After moving evens back to stack2 (in original order):
stack1: []
stack2: [2, 4, 6, 8, 10]
The value at the top of stack2: 10
stack2: [2, 4, 6, 8, 10]
stack1 is empty.
stack2 is not empty.
After adding the odd single-digit numbers to stack1:
stack1: [1, 3, 5, 7, 9]
stack2: [2, 4, 6, 8, 10]
After moving items from stack1 and stack2 (interleaved) to stack3:
stack1: []
stack2: []
stack3: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
After moving each item from stack3 to stacks 1 and 2:
stack1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
stack2: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
stack3: []
I DID NOT POST THE OTHER FILES THAT ARE NEEDED. THIS IS ONLY FOR THE useStack.py FILE! I WILL RATE YOU! THANKS!
Explanation / Answer
Here is the answer as below.
# This program exercises stacks.
# Replace any "yourcode" comments with your own code statement(s)
# to accomplish the specified task.
# Do not change any other code.
# The following files must be in the same folder:
# abstractcollection.py
# abstractstack.py
# arraystack.py
# arrays.py
# linkedstack.py
# node.py
from arraystack import ArrayStack
from linkedstack import LinkedStack
def printStack1():
print("stack1:", stack1)
print()
def printStack2():
print("stack2:", stack2)
print()
def print2Stacks():
print("stack1:", stack1)
print("stack2:", stack2)
print()
def print3Stacks():
print("stack1:", stack1)
print("stack2:", stack2)
print("stack3:", stack3)
print()
# Here are 2 starting stacks:
stack1 = ArrayStack([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
stack2 = ArrayStack([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# Print the stacks:
print2Stacks()
# Part 1:
# Use the == comparison operator to determine if the 2 stacks are equal.
# If they are equal print "The stacks are equal.".
# If they are not equal print "The stacks are not equal."
#yourcode
if stack1 == stack2:
print("The stacks are equal.")
else:
print("The stacks are not equal.")
print()
# Part 2:
# Remove the top item from stack1, print the removed item, and print stack1:
#yourcode
top_item = stack1.pop()
print(top_item)
print("After removing the top item from stack1:")
printStack1()
# Part 3:
# Use the == comparison operator to determine if the 2 stacks are equal.
# If they are equal print "The stacks are equal.".
# If they are not equal print "The stacks are not equal."
#yourcode
if stack1 == stack2:
print("The stacks are equal.")
else:
print("The stacks are not equal.")
print()
# Part 4:
# Remove all the items from stack1 until there is only 3 items left in it:
#yourcode
while(len(stack1)>3 ):
stack1.pop()
print("After removing all but 3 items from stack1:")
printStack1()
# Part 5:
# Use a single method to empty stack1:
#yourcode
stack1.clear()
print("After emptying stack1:")
printStack1()
# Part 6:
# Use pop() and push() to move all even valued items from stack2 to stack1.
# This will leave stack2 empty.
# This will leave stack1 with only even valued items.
# stack1 will be in the reverse order from the original stack2 order.
# When popping, use a try/except block to catch and ignore the KeyError exception.
#yourcode
while(len(stack2) > 0):
try:
item = stack2.pop()
except:
print('Oops! Trying to pop empty stack.')
break
if item % 2 == 0:
stack1.push(item)
print("After moving evens to stack1 (in reverse order):")
print2Stacks()
# Part 7:
# Use pop() and push() to move all the stack1 items back to stack2.
# This will leave stack1 empty.
# This will leave stack2 with the even valued items back in their original order.
# You have effectively removed all the odd valued items from stack2.
# You will again need a try/except block.
#yourcode
while(len(stack1) > 0):
try:
item = stack1.pop()
except:
print('Oops! Trying to pop empty stack.')
break
stack2.push(item)
print("After moving evens back to stack2 (in original order):")
print2Stacks()
# Part 8:
# Get and print the value at the top of stack2 without removing it:
#yourcode
item = stack2[-1]
print("The value at the top of stack2:", item)
printStack2()
# Part 9:
# Use isEmpty() to check whether stack1 and stack2 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.
#yourcode
if stack1.isEmpty():
print("stack1 is Empty.")
else:
print("stack1 is not Empty.")
if stack2.isEmpty():
print("stack2 is Empty.")
else:
print("stack2 is not Empty.")
print()
# Part 10:
# Add the odd single-digit numbers to stack1 with 9 at the top:
#yourcode
odd=1
while(odd<=9 ):
stack1.push(odd)
odd = odd +2
print("After adding the odd single-digit numbers to stack1:")
print2Stacks()
# Part 11:
# Create a new empty stack of type LinkedStack called stack3:
stack3 = LinkedStack()
# Alternate popping items from stack2 and stack1, interleaving them onto stack3.
# Both stacks 1 and 2 will be left empty.
# As usual, handle or avoid exceptions.
#yourcode
cond=1
while(cond == 1):
if(len(stack2) > 0):
try:
item = stack2.pop()
stack3.push(item)
except:
print('Oops! Trying to pop empty stack.')
cond=0 # to break the loop
else:
cond=0 # to break the loop
if(len(stack1) > 0):
try:
item = stack1.pop()
stack3.push(item)
except:
print('Oops! Trying to pop empty stack.')
cond=0 # to break the loop
else:
cond=0 # to break the loop
print("After moving items from stack1 and stack2 (interleaved) to stack3:")
print3Stacks()
# Part 12:
# Move each item from stack3 to both stack1 and stack2.
# Stacks 1 and 2 will be left in their original starting order.
# stack3 will be left empty.
#yourcode
while(len(stack3) > 0):
item=stack3.pop()
stack1.push(item)
stack2.push(item)
print("After moving each item from stack3 to stacks 1 and 2:")
print3Stacks()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.