class PriorityQueue:
def __init__(self):
self.q = []
def enqueue(self, priority, item):
self.q.append([priority, item])
self.q = sorted(self.q)
return self.q
x = PriorityQueue()
print(x.enqueue(3, "Potato"))
y = PriorityQueue()
print(y.enqueue(1, "Egg"))
I'm trying to do a priority list but it won't sort.
output;
[[3, 'Potato']]
[[1, 'Egg']]
How do I fix this?
CodePudding user response:
You are always creating a new PriorityQueue
, I guess you want to have one queue:
class PriorityQueue:
def __init__(self):
self.q = []
def enqueue(self, priority, item):
self.q.append([priority, item])
self.q = sorted(self.q)
return self.q
y = PriorityQueue()
print(y.enqueue(3, "Egg"))
print(y.enqueue(4, "Potato"))
print(y.enqueue(2, "Chesse"))
print(y.enqueue(1, "Cake"))
Out:
[[3, 'Egg']]
[[3, 'Egg'], [4, 'Potato']]
[[2, 'Cheese'], [3, 'Egg'], [4, 'Potato']]
[[1, 'Cake'], [2, 'Cheese'], [3, 'Egg'], [4, 'Potato']]
CodePudding user response:
In this code you have a Class -> PriorityQueue
which inherits a list "q"
.
Now the code creates two Objects of Type PriorityQueue
x and y who are independent from each other.
This also means that their respective list "q"
are independent from each other.
Test out the code below. It will make the entire process more vivid
class PriorityQueue:
def __init__(self):
self.q = []
def enqueue(self, priority, item):
self.q.append([priority, item])
self.q = sorted(self.q)
return self.q
grocerys = PriorityQueue()
print(grocerys.enqueue(3, "Potato"))
print(grocerys.enqueue(6, "Milk"))
print(grocerys.enqueue(1, "Egg"))
print("this is your grocery.q list", grocerys.q)
media = PriorityQueue()
print(media.enqueue(1, "PC"))
print(media.enqueue(3, "Television"))
print(media.enqueue(2, "Moonboard"))
print("this is your media.q list", media.q)
CodePudding user response:
You have 2 separate objects. And you are enqueing in those two objects. That's why you get separate queues nothing surprising here.
class PriorityQueue:
def __init__(self):
self.q = []
def enqueue(self, priority, item):
self.q.append([priority, item])
self.q.sort(key=lambda x:x[0])
return self.q
x = PriorityQueue()
print(x.enqueue(3, "Potato"))
print(x.enqueue(1, "Egg"))
Output:
[[3, 'Potato']]
[[1, 'Egg'], [3, 'Potato']]
You are sorting the list again and again. This is not the right way to implement priority queue. The better way would be to use a heap.
Here is a sample example:
import heapq
h = []
h.append([3, "Potato"])
h.append([1, "Egg"])
heapq.heapify(h)
print(h)
Output:
[[1, 'Egg'], [3, 'Potato']]
To pop from the heap you would use heapq.heappop(h)
etc. Check the docs for further info.
CodePudding user response:
class PriorityQueue:
def __init__(self):
self.items = []
def size(self):
return len(self.items)
def enqueue(self, item):
self.items.append(item)
self.items.sort()
def dequeue(self):
if self.size() == 0:
return None
return self.items.pop(0)
def __repr__(self):
return f"{[item for item in self.items]}"
if __name__ == '__main__':
q = PriorityQueue()
q.enqueue([3, "Potato"])
q.enqueue([1, "Egg"])
print(q)
item_to_process = q.dequeue()
print(item_to_process)
result
[[1, Egg], [3, Potato]]
[1, 'Egg']