Question

In: Computer Science

Please write in Python(Python3) Stack: The following was done already in the Lab in the Stacks...

Please write in Python(Python3)

Stack: The following was done already in the Lab in the Stacks Module with Doubly Linked List.(***Code Below***)

Create an application to help you stack and un-stack containers in the ship.

Create a class called container which will have the object (data), the link (next)

Create a class called Pod which is Stack. Include methods addContainer and removeContainer

Implement these classes by creating multiple containers to go inside the pod.

ADD the Following feature:(*This is what I need to ADD to the code.)

Include a class attribute in the container class called name.

In the implementation - Pod:

You should ask the user to enter the name of the container and the program should add the container to the Pod repeatedly.

Once done, You should ask the user which container to remove from the Pod. When the user gives the name of the container, the program should go through the Pod and remove that container alone from the Pod.

Clue: Remember if you want to remove one item from the Stack, you should use only the top to remove. So if you want to remove the fifth item from the top of the Stack, you should remove all the four items above it, remove the fifth item and then put back all the four items in the same order.

Write a method called removeNamedContainer in Pod program for implementing the above.

# DSTACK #
#****************************************************
# the container
class Container:

# initialise the container object
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# The pod
class Pod:
stack=[]
# push item to the stack
def addContainer(self, data):
c = Container(data)
c.next = None
self.stack.append(c)
# set the next item and prevous
length = len(self.stack)
# print(str(length))
if length>1:
self.stack[length-2].next=self.stack[length-1]
self.stack[length-1].prev=self.stack[length-2]
# pop the stack
def removeContainer(self):
self.stack.pop()
# reset the next link
length = len(self.stack)
self.stack[length-1].next=None
# to print items
def printList(self):
print("Traversal in forward direction")
temp = self.stack[0]
while (temp):
print(temp.data)
temp = temp.next
print("Traversal in reverse direction")
temp = self.stack[len(self.stack)-1]
while (temp):
print(temp.data)
temp = temp.prev
break

# Testing the stack
print("Adding containers...")
pod = Pod()
pod.addContainer(1)
pod.addContainer(2)
pod.addContainer(3)
pod.printList()
# poping the stack
print("Removing containers...")
pod.removeContainer()
pod.printList()

Solutions

Expert Solution

# container class containing data and next attributes

class container():

def _init__(self,data):

self.data=data

self.next=None

#pod class containing addcontainer and removecontainer methods

class pod():

# addcontainer

def addContainer(self,head,containerObj):

# check if intial container exists to stack contianers

if(head):

temp = head

while(temp.next):

temp=temp.next

temp.next=containerObj

return head

else:

return containerObj

# removecotainer

def removeContainer(self,head):

# removing container in stack manner such that the container which is on top

# removed first

if(head.next):

temp = head

prev = None

while(temp.next):

prev=temp

temp=temp.next

prev.next=None

return head

else:

return None

# method to list all the available containers and ships

def helper(head):

while(head):

print(head.data)

head=head.next

print("working with container and pod")

# containers

container_one = container("container_1")

container_two = container("container_2")

container_three = container("container_3")

container_four = container("container_4")

# pod object

podObj = pod()

# adding containers to pod

head = pod Obj.add Container(Non,container one)

head = podObj.addContainer(head,container_two)

head = podobj.addContainer(head, container_three)

head = podobj.addContainer(head,container_four)

# listing all the containeer availabl to cross check

print("ALI the added containeers")

helper(head)

#removind containers from stack so it results in removing

#container_4 and container_3

head = podObj.removeContainer(head)

head = podObj.removeContainer(head)

# listing all the containeer availabl to cross check

print("left over containeers after removing few in stack manner(LIFO)")

helper(head)

# ship class containing data and next attributes

class ship():

def __init__(self,data):

self.data=data

self.next=None

class Port():

# adding ships to dock

def dock(self,head, ship Obj):

if(head):

temp=head

while(temp.next)

temp=temp.next

temp.next=shipObj

return head

else:

return shipObj

# undocking ships in first come first out manner

def undock(self,head):

if(head):

temp = head.next

head.next=None

head=temp

return head

else:

return None

print("working with ship and Port")

# ships

ship_one = ship("ship_1")

ship_two = ship("ship_2")

ship_three = ship("ship_3")

ship four = ship("ship_4")

portObj = Port()

# dock ships

head = portObj.dock(None,ship_one)

head = portObj.dock(head, ship_two)

head = portObj.dock(head, ship_three)

head = portObj.dock(head, ship_four)

# listing all the ships available to cross check

print("All the ships available in port")

helper(head)

# undock ships from the port it results in removing

#ship_1 and ship_2 as the first ship in queue is served first

head = portObj.undock(head)

head = portObj.undock(head)

# listing all the ships available to cross check

print("All the ships available in port after undocking few in Queue manner (FIFO)")

helper(head)


Related Solutions

Python Stack: The following was done already in the Lab in the Stacks Module with Doubly...
Python Stack: The following was done already in the Lab in the Stacks Module with Doubly Linked List. Create an application to help you stack and un-stack containers in the ship. Create a class called container which will have the object (data), the link (next) Create a class called Pod which is Stack. Include methods addContainer and removeContainer Implement these classes by creating multiple containers to go inside the pod. ADD the Following feature: Include a class attribute in the...
In Java Please!!! 6.22 LAB: Python and sqlite basics Write a Python program that connects to...
In Java Please!!! 6.22 LAB: Python and sqlite basics Write a Python program that connects to a sqlite database. Create a table called Horses with the following fields: id (integer): a primary key and not null name (text) breed (text) height (real) birthday (text) Next, insert the following data row into the Horses table: id: 1 name: 'Babe' breed: 'Quarter Horse' height: 15.3 birthday: '2015-02-10' Output all records from the Horses table. Ex: With the above row inserted, the output...
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program:...
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program: Your program will create a username of your choice using a Kali Linux command "useradd -m mark", then set the password for that user using the Kali Linux Command "echo "mark:10101111" | chpasswd". Then you create a dictionary file using the Kali Linux command "crunch 8 8 01 > mylist.txt" Your python script should crack the password for that user and display on the...
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
In C language using stacks: Activity: Using Stack, check whether the following is balanced or not...
In C language using stacks: Activity: Using Stack, check whether the following is balanced or not : “{ ( ) } ) ” -Assume a stack of Max_Size = 6 -Draw the stack for each step and show the value of “top” -If any decision is made (valid/invalid), then write the reason 11
using python3 Write a Python program that lets a user search through the 'data.txt' file for...
using python3 Write a Python program that lets a user search through the 'data.txt' file for a given first name, last name, or email address, and print all the matches/results. Prompt the user for which field to search, (f) for first name, (l) for last name, or (e) for email data.txt entries are all formatted as: first_name, last_name, email, separated by TABS Your program should not read the entire file into memory, it should read line by line, and only...
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write...
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write a non-recursive function fib() which: ·Takes a single int parameter n ·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2) ·To compute this without using recursion, we use a stack to store the numbers we need to compute (1)   Initialize your result to be 0 (2)   Push the parameter n onto the stack (3)   While the...
Suppose the interface and the class of stack already implemented, Write application program to 1- insert...
Suppose the interface and the class of stack already implemented, Write application program to 1- insert 100 numbers to the stack 2- Print the even numbers 3- Print the summation of the odd numbers
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then,...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents: Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 8: Assignment 1 # ************************************************* #...
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT