Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# This program exercises stacks. # Replace any yourcode comments with your own code statement(s) # to accomplish the specified task. # Do not change

# 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!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

What tools might be helpful?

Answered: 1 week ago