diff --git a/main.py b/main.py index 5a3e755..940e33a 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,12 @@ from priority_queue import PriorityQueue def main(): - pq = PriorityQueue() - - pq.push("Task 1", 3) - pq.push("Task 2", 1) - pq.push("Task 3", 2) - - print(pq.pop()) - print(pq.pop()) - print(pq.pop()) + pq = PriorityQueue() + pq.push("Task 1", 3) + pq.push("Task 2", 1) + pq.push("Task 3", 2) + print(pq.pop()) + print(pq.pop()) + print(pq.pop()) if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/priority_queue.py b/priority_queue.py index 6994ec7..f54f6be 100644 --- a/priority_queue.py +++ b/priority_queue.py @@ -10,11 +10,11 @@ def push(self, item, priority): def pop(self): if len(self.heap) > 1: self._swap(0, len(self.heap) - 1) - priority, item = self.heap.pop() + _, item = self.heap.pop() self._sift_down(0) return item elif len(self.heap) == 1: - priority, item = self.heap.pop() + _, item = self.heap.pop() return item else: return None @@ -35,11 +35,11 @@ def _sift_down(self, index): smallest = index if left_child_index < len(self.heap) and \ - self.heap[left_child_index][0] > self.heap[smallest][0]: + self.heap[left_child_index][0] < self.heap[smallest][0]: smallest = left_child_index if right_child_index < len(self.heap) and \ - self.heap[right_child_index][0] > self.heap[smallest][0]: + self.heap[right_child_index][0] < self.heap[smallest][0]: smallest = right_child_index if smallest != index: diff --git a/test.py b/test_.py similarity index 100% rename from test.py rename to test_.py