def __init__(self): """ ------------------------------------------------------- Initializes an empty priority queue. Use: pq = Priority_Queue() ------------------------------------------------------- Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌: a new Priority_Queue object (Priority_Queue) ------------------------------------------------------- """ self._values = [] self._first = None   defis_empty(self): """ ------------------------------------------------------- Determines if the priority queue is empty. Use: b = pq.is_empty() ------------------------------------------------------- Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌: True if priority queue is empty, False otherwise. ------------------------------------------------------- """ return len(self._values) == 0   def__len__(self): """ ------------------------------------------------------- Returns the length of the priority queue. Use: n = len(pq) ------------------------------------------------------- Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌: the number of values in the priority queue. ------------------------------------------------------- """ return len(self._values)   definsert(self, value): """ ------------------------------------------------------- A copy of value is appended to the end of the the priority queue Python list, and _first is updated as appropriate to the index of value with the highest priority. Use: pq.insert(value) ------------------------------------------------------- Parameters: value - a data element (?) Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌: None ------------------------------------------------------- """ self._values.append(deepcopy(value))   # Determine whether new value has highest priority. ifself._first isNone: self._first = 0 elif value < self._values[self._first]: self._first = len(self._values) - 1 return   def_set_first(self): """ ------------------------------------------------------- Private helper function to set the value of _first. _first is the index of the value with the highest priority in the priority queue. None if queue is empty. Use: self._set_first() ------------------------------------------------------- Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌: None ------------------------------------------------------- """ n = len(self._values)   if n == 0: self._first = None else: self._first = 0   for i in range(1, n): ifself._values[i] < self._values[self._first]: self._first = i return   defremove(self): """ ------------------------------------------------------- Removes and returns the highest priority value from the priority queue. Use: value = pq.remove() ------------------------------------------------------- Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌: value - the highest priority value in the priority queue - the value is removed from the priority queue. (?) ------------------------------------------------------- """ assert len(self._values) > 0, "Cannot remove from an empty priority queue"   value = self._values.pop(self._first) # Find the value with the next highest priority. self._set_first() return value   defpeek(self): """ ------------------------------------------------------- Peeks at the highest priority value of the priority queue. Use: v = pq.peek() ------------------------------------------------------- Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌: value - a copy of the highest priority value in the priority queue - the value is not removed from the priority queue. (?) ------------------------------------------------------- """ assert len(self._values) > 0, "Cannot peek at an empty priority queue"   value = deepcopy(self._values[self._first])   return value   def__iter__(self): """ FOR TESTING ONLY ------------------------------------------------------- Generates a Python iterator. Iterates through the priority queue from front to rear. Not in priority order. Use: for value in pq: ------------------------------------------------------- Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌: value - the next value in the priority queue (?) ------------------------------------------------------- """ for value inself._values: yield value

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

def __init__(self):

"""

-------------------------------------------------------

Initializes an empty priority queue.

Use: pq = Priority_Queue()

-------------------------------------------------------

Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌:

a new Priority_Queue object (Priority_Queue)

-------------------------------------------------------

"""

self._values = []

self._first = None

 

defis_empty(self):

"""

-------------------------------------------------------

Determines if the priority queue is empty.

Use: b = pq.is_empty()

-------------------------------------------------------

Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌:

True if priority queue is empty, False otherwise.

-------------------------------------------------------

"""

return len(self._values) == 0

 

def__len__(self):

"""

-------------------------------------------------------

Returns the length of the priority queue.

Use: n = len(pq)

-------------------------------------------------------

Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌:

the number of values in the priority queue.

-------------------------------------------------------

"""

return len(self._values)

 

definsert(self, value):

"""

-------------------------------------------------------

A copy of value is appended to the end of the the priority queue

Python list, and _first is updated as appropriate to the index of

value with the highest priority.

Use: pq.insert(value)

-------------------------------------------------------

Parameters:

value - a data element (?)

Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌:

None

-------------------------------------------------------

"""

self._values.append(deepcopy(value))

 

# Determine whether new value has highest priority.

ifself._first isNone:

self._first = 0

elif value < self._values[self._first]:

self._first = len(self._values) - 1

return

 

def_set_first(self):

"""

-------------------------------------------------------

Private helper function to set the value of _first.

_first is the index of the value with the highest

priority in the priority queue. None if queue is empty.

Use: self._set_first()

-------------------------------------------------------

Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌:

None

-------------------------------------------------------

"""

n = len(self._values)

 

if n == 0:

self._first = None

else:

self._first = 0

 

for i in range(1, n):

ifself._values[i] < self._values[self._first]:

self._first = i

return

 

defremove(self):

"""

-------------------------------------------------------

Removes and returns the highest priority value from the priority queue.

Use: value = pq.remove()

-------------------------------------------------------

Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌:

value - the highest priority value in the priority queue -

the value is removed from the priority queue. (?)

-------------------------------------------------------

"""

assert len(self._values) > 0, "Cannot remove from an empty priority queue"

 

value = self._values.pop(self._first)

# Find the value with the next highest priority.

self._set_first()

return value

 

defpeek(self):

"""

-------------------------------------------------------

Peeks at the highest priority value of the priority queue.

Use: v = pq.peek()

-------------------------------------------------------

Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌:

value - a copy of the highest priority value in the priority queue -

the value is not removed from the priority queue. (?)

-------------------------------------------------------

"""

assert len(self._values) > 0, "Cannot peek at an empty priority queue"

 

value = deepcopy(self._values[self._first])

 

return value

 

def__iter__(self):

"""

FOR TESTING ONLY

-------------------------------------------------------

Generates a Python iterator. Iterates through the priority queue

from front to rear. Not in priority order.

Use: for value in pq:

-------------------------------------------------------

Returns‌​‌​​​​‌​​‌‌​​​‌​​​​‌‌​‌​‌‌‌:

value - the next value in the priority queue (?)

-------------------------------------------------------

"""

for value inself._values:

yield value

7 # Imports
9
2
B
9
from copy import deepcopy
class Priority_Queue:
def strip_key (self, kev):
Removes all values from self that have a priority
less than key.
Use: source.strip_key (key)
Parameters:
key
Returns:
None
||||||
return
a key value (?)
CHANCE
Transcribed Image Text:7 # Imports 9 2 B 9 from copy import deepcopy class Priority_Queue: def strip_key (self, kev): Removes all values from self that have a priority less than key. Use: source.strip_key (key) Parameters: key Returns: None |||||| return a key value (?) CHANCE
Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education