Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python code: In this assignment, you will work with stacks. Begin with the useStack.py starter file. This file contains comment instructions that tell you where

Python code:

In this assignment, you will work with stacks.

Begin with the "useStack.py" starter file. This file contains comment instructions that tell you where to add your code to do various tasks that use and manipulate stacks.

Make sure you read the " Guidance" document.

Make sure your output matches the "Expected Output" document.

May you make sure it's compiling right?

Starter file:

File: useStack.py

This program exercises stacks.

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()

Guidance: ================================= Adding, removing, and moving items ---------------------------------- When you are instructed to add an item to a stack, your only option is to add it to the top of the stack. To add an item to a stack, you must use push(). When you are instructed to remove an item from a stack, your only option is to remove it from the top of the stack. To remove an item from a stack, you must use pop(). When you are instructed to move items from one stack to another, as you remove them from the source stack, you add them to the destination stack. For multiple items, this must be done within a loop. Below are two loop patterns for multiple item movement. They expect your code to remove all the items from a source stack. One item is removed during each iteration of the loop. After the last source item is removed, the loop will be done. If your loop does not remove all the items, it will be an infinite loop. That would cause your program to appear to freeze and do nothing. But actually, it would be running your loop over and over forever. You will have to manually terminate your program if you cause this bug. while (len(sourceStack > 0)): #  or while (True): try: #  except KeyError: break Part 1 ------ Many of the output statements are already provided in the starter file. Some parts, such as this one, require you to provide some of the output statements. Make sure your output matches that shown in "Programming Activity 4 - Expected Output" document. Part 7 ------ To check whether an item is an even number, you can use: if (item % 2 == 0): Part 11 ------- You can use a "for" loop with a controller of the form: for i in range(start, stop, step): where you supply the proper values for start, stop, and step. Part 12 ------- Alternate removing items from stack3 and stack1, interleaving them onto stack2 means to follow the pattern: Remove an item from stack3 and put it on stack2. Remove an item from stack1 and put it on stack2. repeating this in a loop. Part 14 ------- You will first need to create the intermediate stack: viaStack = ArrayStack() Then you will use 2 loops, one after the other. They are not nested loops. The first loop must move all the items from stack3 to viaStack. The secomd loop must move all the items from viaStack to stack2. 

Expected output:

stack1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] stack2: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] stack3: [] stack1 and stack2 are not equal. After moving each item from stack2 to stack3: stack1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] stack2: [] stack3: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] stack1 and stack3 are equal. Removed item: 10 After removing the top item from stack1: stack1: [1, 2, 3, 4, 5, 6, 7, 8, 9] stack2: [] stack3: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] After removing all but 2 items from stack1: stack1: [1, 2] stack2: [] stack3: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] After emptying stack1: stack1: [] stack2: [] stack3: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] After moving evens from stack3 to stack1: stack1: [10, 8, 6, 4, 2] stack2: [] stack3: [] After moving evens back to stack3: stack1: [] stack2: [] stack3: [2, 4, 6, 8, 10] The value at the top of stack3: 10 stack1: [] stack2: [] stack3: [2, 4, 6, 8, 10] stack1 is empty. stack3 is not empty. After adding the odd single-digit numbers to stack1: stack1: [1, 3, 5, 7, 9] stack2: [] stack3: [2, 4, 6, 8, 10] After moving items from stack3 and stack1 (interleaved) to stack2: stack1: [] stack2: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] stack3: [] After moving each item from stack2 to stacks 1 and 3: stack1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] stack2: [] stack3: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] After moving each item from stack3 to stack2, preserving their order: stack1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] stack2: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] stack3: []

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 Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions

Question

Whose religious guru did Jahangir make harsh remarks about?

Answered: 1 week ago

Question

4. Choose appropriate and powerful language

Answered: 1 week ago

Question

2. Choose an appropriate organizational pattern for your speech

Answered: 1 week ago