In Python, implement a container class PriorityQueue (that is a subclass of the
ID: 3837543 • Letter: I
Question
In Python, implement a container class PriorityQueue (that is a subclass of the object class) supporting nine methods:
__init__ which takes no parameter and initializes the object to an empty queue
insert which takes a number as a parameter and inserts the number into the queue, modifying the queue afterward to insure that the number is placed in the correct position
minimum which takes no parameters and returns the smallest number in the queue
removeMin which takes no parameters and removes the smallest number from the queue, returning the value removed
__len__ which takes no parameters and returns the size of the priority queue
__str__ which takes no parameters and returns a string representing the queue
__repr__ which takes no parameters and returns the canonical representation of the queue
__getitem__ which takes an index as a parameter and returns the queue item at that index without modifying the queue
__iter__ which implements iteration for the queue -- iteration should proceed from the smallest to the largest number in the queue
I suggest that you use a list to store the numbers in the priority queue. Your implementation must insure that the list is ordered after every operation (insertion or removal) so that the minimum number is stored in one end of the list. The following shows how the PriorityQueue class and its methods could be used. Note that in my implementation the queue is stored in non-decreasing order (i.e. with the minimum at the front of the queue). You can also write an implementation that stores the queue items in non-increasing order (i.e. with the minimum at the end of the queue). Either implementation is fine, although your choice will impact the way that you write the __getitem__ and __iter__ methods:
Explanation / Answer
# cook your code here
class PriorityQueue :
def __init__(self):
PriorityQueue.qu = [];
def insert(self,num):
PriorityQueue.qu.append(num);
PriorityQueue.qu.sort();
print(PriorityQueue.qu)
def minimum(self):
return min(PriorityQueue.qu);
def removeMin(self):
PriorityQueue.qu.pop(0)
def __len__ (self):
return len(PriorityQueue.qu);
def __str__ (self):
str = ''";
for val in (PriorityQueue.qu):
str += `val` ;
return str;
def __repr__ (self):
str = ''";
for val in (PriorityQueue.qu):
str += `val` + '-';
return str.rstrip('-');
def __getitem__ (self, index):
return PriorityQueue.qu[index:index+1]
def __iter__(self):
for val in PriorityQueue.qu:
print val
def main():
ob = PriorityQueue(); //creating object of PreorityQueue
ob.insert(4);
ob.insert(52);
ob.insert(12);
print(ob.minimum());
print(ob.removeMin());
print(ob.__str__());
print(ob.__repr__());
print(ob.__getitem__(1));
print(ob.__iter__()); //End of main
main(); //Calling main Methode
########### ######################
Hope this is what you are looking for. Main methode definition and calling of main() you can put in your parrent class, if you want any.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.