Problem 1 An animal shelter holds only dogs and cats, and operates on a strictly "first in, first out" basis. People must adopt either the "oldest" (based on arrival time) of all animals at the...

1 answer below »
Please see instructions for this assignment in the word document. You need to follow the instructions carefully and meet all grading requirements. Thank you.


Problem 1 An animal shelter holds only dogs and cats, and operates on a strictly "first in, first out" basis. People must adopt either the "oldest" (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data structures to maintain this system and implement operations such as enqueue, dequeueAny, dequeueDog and dequeueCat. Assume each animal has an unique name and thus you don’t need to worry. You need to use at most O(n) storage and O(1) for all operations, where n is the total number of animals in the shelter. Please name your program p8 and you need try all possible tests to make sure your p8 works. Your program should read all t8*.dat in the same directory as your p8.py or p8.ipynb, and generate output for each t8*.dat as the example below. You need handle whitespace as the whitespace convention and handle errors as always. You can only handle cats and dogs, otherwise, print error messages and continue for next file if exists. You also need to handle boundary condition, e.g., if dequeue or dequeuAny with no animal left, you should print warning messages like "no more dog/cat left to be adopted". Boundary conditions are counted as correctness points. Example Input and Output (as comments below) in chronological order: enqueue(dog, Bark)     # put the dog named Bark to shelter enqueue(cat, Mimi)     # put the cat named Mimi to shelter enqueue(cat, Tiger)     # put the cat named Tiger to shelter dequeue(cat)                   # the cat named Mimi is adopted dequeueAny()                 # the dog named Bark is adopted dequeue(dog)                # no dog left to be adopted Score: Correctness and boundary condition (60%): Whitespace convention (5%) Error Handling (5%): Big O notations for all functions: (5%) Modular design, documentation (25%)
Answered Same DayOct 25, 2021

Answer To: Problem 1 An animal shelter holds only dogs and cats, and operates on a strictly "first in, first...

Arun Shankar answered on Oct 27 2021
152 Votes
main.py
class Queue:
def __init__(self):
self.catsCount = 0
self.cats = []
self.
dogsCount = 0
self.dogs = []
def enqueueCat(self, animal):
self.cats.append(animal)
self.catsCount += 1
animal.setArrivalTime(self.catsCount + self.dogsCount)
print("put the cat named " + animal.getName() + " to the shelter")
def enqueueDog(self, animal):
self.dogs.append(animal)
self.dogsCount += 1
animal.setArrivalTime(self.catsCount + self.dogsCount)
print("put the dog named " + animal.getName() + " to the shelter")
def dequeueAny(self):
if(self.catsCount + self.dogsCount == 0):
print("Nothing to dequeue")
elif(self.catsCount == 0):
self.dequeueDog()
elif(self.dogsCount == 0):
self.dequeueCat()
else: # check whether the dog is older, or the cat
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here