Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ jobs:
pylint *.py
- name: Test with pytest
run: |
pytest
python test.py
20 changes: 10 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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()
main()
8 changes: 4 additions & 4 deletions priority_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down