mirror of
https://github.com/Andreabont/PyExperiments.git
synced 2024-09-19 14:43:05 +00:00
29 lines
606 B
Python
29 lines
606 B
Python
|
import heapq
|
||
|
import collections
|
||
|
|
||
|
class Queue:
|
||
|
def __init__(self):
|
||
|
self.elements = collections.deque()
|
||
|
|
||
|
def empty(self):
|
||
|
return len(self.elements) == 0
|
||
|
|
||
|
def put(self, x):
|
||
|
self.elements.append(x)
|
||
|
|
||
|
def get(self):
|
||
|
return self.elements.popleft()
|
||
|
|
||
|
class PriorityQueue:
|
||
|
def __init__(self):
|
||
|
self.elements = []
|
||
|
|
||
|
def empty(self):
|
||
|
return len(self.elements) == 0
|
||
|
|
||
|
def put(self, item, priority):
|
||
|
heapq.heappush(self.elements, (priority, item))
|
||
|
|
||
|
def get(self):
|
||
|
return heapq.heappop(self.elements)[1]
|