Skip to content

Commit a22c7b4

Browse files
committed
init
0 parents  commit a22c7b4

File tree

6 files changed

+579
-0
lines changed

6 files changed

+579
-0
lines changed

‎.github/workflows/python-app.yml‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
3+
4+
name: Python application
5+
6+
on:
7+
push:
8+
branches: [ "main" ]
9+
pull_request:
10+
branches: [ "main" ]
11+
workflow_dispatch:
12+
inputs:
13+
tags:
14+
description: "Test python build"
15+
required: false
16+
type: string
17+
18+
19+
20+
permissions:
21+
contents: read
22+
23+
jobs:
24+
build:
25+
26+
runs-on: ubuntu-latest
27+
28+
steps:
29+
- uses: actions/checkout@v3
30+
- name: Set up Python 3.10
31+
uses: actions/setup-python@v3
32+
with:
33+
python-version: "3.10"
34+
- name: Install dependencies
35+
run: |
36+
python -m pip install --upgrade pip
37+
pip install pylint pytest
38+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
39+
- name: Lint with pylint
40+
run: |
41+
pylint *.py
42+
- name: Test with pytest
43+
run: |
44+
pytest

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.pytest_cache
2+
__pycache__

‎main.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from priority_queue import PriorityQueue
2+
def main():
3+
pq = PriorityQueue()
4+
5+
pq.push("Task 1", 3)
6+
pq.push("Task 2", 1)
7+
pq.push("Task 3", 2)
8+
9+
print(pq.pop())
10+
print(pq.pop())
11+
print(pq.pop())
12+
13+
if __name__ == "__main__":
14+
main()

‎priority_queue.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class PriorityQueue:
2+
def __init__(self):
3+
self.heap = []
4+
5+
def push(self, item, priority):
6+
entry = (priority, item)
7+
self.heap.append(entry)
8+
self._sift_up(len(self.heap) - 1)
9+
10+
def pop(self):
11+
if len(self.heap) > 1:
12+
self._swap(0, len(self.heap) - 1)
13+
priority, item = self.heap.pop()
14+
self._sift_down(0)
15+
return item
16+
elif len(self.heap) == 1:
17+
priority, item = self.heap.pop()
18+
return item
19+
else:
20+
return None
21+
22+
def _sift_up(self, index):
23+
while index > 0:
24+
parent_index = (index - 1) // 2
25+
if self.heap[parent_index][0] > self.heap[index][0]:
26+
self._swap(parent_index, index)
27+
index = parent_index
28+
else:
29+
break
30+
31+
def _sift_down(self, index):
32+
while True:
33+
left_child_index = 2 * index + 1
34+
right_child_index = 2 * index + 2
35+
smallest = index
36+
37+
if left_child_index < len(self.heap) and \
38+
self.heap[left_child_index][0] > self.heap[smallest][0]:
39+
smallest = left_child_index
40+
41+
if right_child_index < len(self.heap) and \
42+
self.heap[right_child_index][0] > self.heap[smallest][0]:
43+
smallest = right_child_index
44+
45+
if smallest != index:
46+
self._swap(index, smallest)
47+
index = smallest
48+
else:
49+
break
50+
51+
def _swap(self, i, j):
52+
self.heap[i], self.heap[j] = self.heap[j], self.heap[i]

0 commit comments

Comments
 (0)