Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
havenkim authored and raacker committed Mar 10, 2024
0 parents commit 8c7b733
Show file tree
Hide file tree
Showing 6 changed files with 579 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:
inputs:
tags:
description: "Test python build"
required: false
type: string



permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with pylint
run: |
pylint *.py
- name: Test with pytest
run: |
pytest
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.pytest_cache
__pycache__
14 changes: 14 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +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())

if __name__ == "__main__":
main()
52 changes: 52 additions & 0 deletions priority_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class PriorityQueue:
def __init__(self):
self.heap = []

def push(self, item, priority):
entry = (priority, item)
self.heap.append(entry)
self._sift_up(len(self.heap) - 1)

def pop(self):
if len(self.heap) > 1:
self._swap(0, len(self.heap) - 1)
priority, item = self.heap.pop()
self._sift_down(0)
return item
elif len(self.heap) == 1:
priority, item = self.heap.pop()
return item
else:
return None

def _sift_up(self, index):
while index > 0:
parent_index = (index - 1) // 2
if self.heap[parent_index][0] > self.heap[index][0]:
self._swap(parent_index, index)
index = parent_index
else:
break

def _sift_down(self, index):
while True:
left_child_index = 2 * index + 1
right_child_index = 2 * index + 2
smallest = index

if left_child_index < len(self.heap) and \
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]:
smallest = right_child_index

if smallest != index:
self._swap(index, smallest)
index = smallest
else:
break

def _swap(self, i, j):
self.heap[i], self.heap[j] = self.heap[j], self.heap[i]
Loading

0 comments on commit 8c7b733

Please sign in to comment.