Question: I have a PYTHON question that I can't seem to solve. The Priority_Queue class maintains a queue where each value put in the queue also

I have a PYTHON question that I can't seem to solve.

The Priority_Queue class maintains a queue where each value put in the queue also has a priority (higher numbers have a higher priority). When you add to the queue, it goes to the back as expected. When you remove from the queue, the highest priority item is removed. If there are multiple values with the same high priority, then the first one (following the FIFO strategy) is removed first. Here are the detailed requirements (which can not be changed):

The enqueue function shall add a node to the back of the queue.

The dequeue function shall remove the node with the highest priority and return its value.

If there are more than one node with the highest priority, then the item closest to the front of the queue will be removed and its value returned.

If the queue is empty, then an error message will be displayed.

Write your own test cases based on these requirements within the test case documentation at the end of the code file. Ensure that your tests cover all of the requirements listed above.

Run your test cases and find the errors. If your tests were not sufficient, you might not find all the errors in the code. You should summarize the results of the tests and the errors found within the test case documentation at the end of the code file.

Fix the code so that all requirements are implemented correctly. You know that you are done because the test cases will all pass (assuming your test cases were sufficient).

class Priority_Queue:

"""

This queue follows the same FIFO process except that higher priority

nodes will be dequeued before lower priority nodes. Nodes of the same

priority will follow the same FIFO process.

"""

# Text value and use number for priority.

class Node:

"""

Each node is the queue will have both a value and a priority.

"""

def __init__(self, value, priority):

"""

Initialize a new node

"""

self.value = value

self.priority = priority

def __str__(self):

"""

Display a single node

"""

return "{0} (Pri:{})".format(self.value, self.priority)

def __init__(self):

"""

Initialize an empty priority queue

"""

self.queue = []

def enqueue(self, value, priority):

"""

Add a new value to the queue with an associated priority. The

node is always added to the back of the queue irregardless of

the priority.

"""

new_node = Priority_Queue.Node(priority, value)

self.queue.append(new_node)

def dequeue(self):

"""

Remove the next value from the queue based on the priority. The

highest priority item will be removed. In the case of multiple

values with the same high priority, the one closest to the front

(in traditional FIFO order) will be removed. Priority values are

interpreted as higher numbers have higher priority. For example,

10 is a higher priority than 5.

"""

if len(self.queue) == 0: # Verify the queue is not empty

print("The queue is empty.")

return None

# Find the index of the item with the highest priority to remove

high_pri_index = 0

for index in range(1, len(self.queue)):

# remove = if self.queue[index].priority >=

if self.queue[index].priority > self.queue[high_pri_index].priority:

high_pri_index = index

# Remove and return the item with the highest priority

# Added remove to live 76

value = self.queue.remove[high_pri_index].value

# Added this line of code

self.queue.pop(high_pri_index)

return value

def __len__(self):

"""

Support the len() function

"""

return len(self.queue)

def __str__(self):

"""

Suppport the str() function to provide a string representation of the

priority queue. This is useful for debugging. If you have a

Priority_Queue object called pq, then you run print(pq) to see the

contents.

"""

# [1,0]

result = "["

for node in self.queue:

result += str(node) # This uses the __str__ from the Node class

result += ", "

result += "]"

return result

# print(Priority_Queue)

# Test Cases

# Test 1

# Scenario:

# Expected Result:

print("Test 1")

P1 = Priority_Queue()

P1.enqueue(2,10)

P1.enqueue(5,50)

print(P1)

# P1.dequeue()

# print(P1)

# P1.enqueue(4,20)

# P1.enqueue(4,30)

# print(P1)

# P1.dequeue()

# print(P1)

# Defect(s) Found:

print("=================")

# Test 2

# Scenario:

# Expected Result:

print("Test 2")

# P2 = Priority_Queue()

# P2.dequeue()

# print(P2)

# P2.enqueue(10,80)

# P2.enqueue(7,60)

# P2.enqueue(8,90)

# P2.enqueue(8,100)

# print(P2)

# P2.dequeue()

# P2.dequeue()

# print(P2)

# Defect(s) Found:

print("=================")

# Add more Test Cases As Needed Below

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!