Skip to content

Commit 387f5fe

Browse files
Refactor for cleaner codebase (#2)
- Break the pathfinding visualizer into smaller files for maintainability. - Break the data structures file into multiple files for maintainability. - Remove unneeded comments.
1 parent 8fed34d commit 387f5fe

30 files changed

+1129
-1131
lines changed

.gitignore

100644100755
File mode changed.

.pre-commit-config.yaml

100644100755
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ repos:
2727
rev: 22.3.0
2828
hooks:
2929
- id: black
30-
language_version: python3.10
31-
args: ['--target-version', 'py310']
3230
- repo: https://github.com/asottile/pyupgrade
33-
rev: v2.19.0
31+
rev: v3.4.0
3432
hooks:
3533
- id: pyupgrade
3634
args: [--py37-plus]

README.md

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ If the script fails, please check out the [setup doc](./docs/setup.md) for instr
2222

2323
## Running The Program
2424

25-
Once you are done with dev setup, enter the virtual environment and simply run `python3 driver.py` to start the program.
25+
Once you are done with dev setup. Enter the virtual environment with `source .venv/bin/activate` if you are on a Mac/Unix/Linux system, or with `.\venv\Scripts\activate` if you are on a Windows system.
26+
Then simply run `python main.py` to start the program.

bin/clean

100644100755
File mode changed.

bin/get_pip

100644100755
File mode changed.

bin/setup

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def is_win():
1414

1515
def check_py_version(major: int, minor: int) -> None:
1616
pyver = sys.version_info
17-
if pyver.major != 3 or pyver.minor != 10:
17+
if pyver.major != major or pyver.minor < minor:
1818
print(
1919
f"Your Python version is {pyver.major}.{pyver.minor}. "
2020
f"Python {major}.{minor} is required!\n"
@@ -50,7 +50,7 @@ def perform_installations() -> None:
5050

5151
def main():
5252
try:
53-
check_py_version(3, 10)
53+
check_py_version(3, 7)
5454
create_venv()
5555
perform_installations()
5656
except:

docs/setup.md

100644100755
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ A complete guide to setup your development environment :D
44

55
---
66

7-
### Requirements
7+
### Requirement
88

9-
`python 3.7` or higher
9+
Minimum `python3.7`. `python 3.10` is recommended.
1010

1111
Make sure you also already installed `pip` and `venv` for your Python
1212

@@ -58,4 +58,4 @@ These lint tools will be run when you commit your code and you can only successf
5858

5959
---
6060

61-
### You're good to run the program or to write your code now! :)
61+
### You're good to run the program and also write your code now! :)

images/algo_icon.ico

100644100755
File mode changed.

images/path_icon.ico

100644100755
File mode changed.

images/readme.md

100644100755
File mode changed.

images/sort_icon.ico

100644100755
File mode changed.

driver.py renamed to main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#!/usr/bin/python3
2-
1+
#!/usr/bin/python
32
from src.home_window import HomeWindow
43

4+
55
if __name__ == "__main__":
66
while True:
77
HomeWindow()

requirements-dev.txt

100644100755
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1+
cfgv==3.3.1
2+
distlib==0.3.6
3+
filelock==3.12.0
4+
identify==2.5.23
5+
nodeenv==1.7.0
6+
platformdirs==2.6.2
17
pre-commit==2.20.0
8+
PyYAML==6.0
9+
toml==0.10.2
10+
virtualenv==20.16.5

requirements.txt

100644100755
File mode changed.

src/data_structures.py

Lines changed: 0 additions & 206 deletions
This file was deleted.

src/data_structures/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .priority_queue import PriorityQueue
2+
from .queue import Queue
3+
from .stack import Stack
4+
5+
__all__ = ["PriorityQueue", "Queue", "Stack"]

src/data_structures/nodes.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Generic
2+
from typing import TypeVar
3+
4+
T = TypeVar("T")
5+
6+
7+
class Node(Generic[T]):
8+
"""Linked List Node"""
9+
10+
def __init__(self, value: T, next=None) -> None:
11+
self._value = value
12+
self._next = next
13+
14+
def get_value(self) -> T:
15+
return self._value
16+
17+
def get_next(self):
18+
return self._next
19+
20+
def set_next(self, next):
21+
self._next = next
22+
23+
24+
class PriorityNode(Node):
25+
"""Priority Node"""
26+
27+
def __init__(self, value, priority: float, next_node=None) -> None:
28+
super().__init__(value, next_node)
29+
self._priority = priority
30+
31+
def get_priority(self) -> float:
32+
return self._priority

src/data_structures/priority_queue.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from typing import Generic
2+
from typing import TypeVar
3+
4+
from .nodes import PriorityNode
5+
6+
T = TypeVar("T")
7+
8+
9+
class PriorityQueue(Generic[T]):
10+
"""Priority Queue"""
11+
12+
def __init__(self) -> None:
13+
self.reset()
14+
15+
# add new node to the queue
16+
# The lower the priority is, the faster it gets to be poped
17+
def enqueue(self, val: T, priority: float = None) -> None:
18+
new_node = PriorityNode(val, priority)
19+
if self.is_empty():
20+
self._head = new_node
21+
self._tail = new_node
22+
else:
23+
if priority is None or priority > self._tail.get_priority():
24+
self._tail.set_next(new_node)
25+
self._tail = new_node
26+
elif priority <= self._head.get_priority():
27+
new_node.set_next(self._head)
28+
self._head = new_node
29+
else:
30+
current = self._head
31+
# find the right place to put the new node in according to the priority
32+
while (
33+
current.get_next() is not None
34+
and current.get_next().get_priority() < priority
35+
):
36+
current = current.get_next()
37+
temp = current.get_next()
38+
current.set_next(new_node)
39+
new_node.set_next(temp)
40+
self._size += 1
41+
42+
# pop the node at the top of the queue
43+
# time complexity: O(1)
44+
def dequeue(self) -> T:
45+
if self.is_empty():
46+
raise Exception("Attempted to dequeue an empty queue!!!")
47+
temp = self.peek()
48+
self._head = self._head.get_next()
49+
self._size -= 1
50+
# if the new head is None, it means the queue is empty
51+
if self._head is None:
52+
self.reset()
53+
return temp
54+
55+
# get the value in the front of the queue
56+
# time complexity: O(1)
57+
def peek(self) -> T:
58+
if self.is_empty():
59+
raise Exception("Attempted to peek an empty queue!!!")
60+
return self._head.get_value()
61+
62+
# get the size of the queue
63+
def get_size(self) -> int:
64+
return self._size
65+
66+
# check if the queue is empty
67+
def is_empty(self) -> bool:
68+
return self._size == 0
69+
70+
def reset(self) -> None:
71+
self._head = None
72+
self._tail = None
73+
self._size = 0

0 commit comments

Comments
 (0)