Skip to content

Commit 3e41fd6

Browse files
committed
Merge branch 'master' of https://github.com/athikha/Python
2 parents b52536b + 0106e5f commit 3e41fd6

23 files changed

+445
-42
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ jobs:
99
build:
1010
runs-on: ubuntu-latest
1111
steps:
12-
- run:
13-
sudo apt-get update && sudo apt-get install -y libtiff5-dev libjpeg8-dev libopenjp2-7-dev
14-
zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python3-tk
15-
libharfbuzz-dev libfribidi-dev libxcb1-dev
16-
libxml2-dev libxslt-dev
17-
libhdf5-dev
18-
libopenblas-dev
12+
- run: sudo apt-get update && sudo apt-get install -y libhdf5-dev
1913
- uses: actions/checkout@v5
2014
- uses: astral-sh/setup-uv@v7
2115
with:
@@ -32,6 +26,7 @@ jobs:
3226
--ignore=computer_vision/cnn_classification.py
3327
--ignore=docs/conf.py
3428
--ignore=dynamic_programming/k_means_clustering_tensorflow.py
29+
--ignore=machine_learning/local_weighted_learning/local_weighted_learning.py
3530
--ignore=machine_learning/lstm/lstm_prediction.py
3631
--ignore=neural_network/input_data.py
3732
--ignore=project_euler/

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ We want your work to be readable by others; therefore, we encourage you to note
9999
ruff check
100100
```
101101

102-
- Original code submission require docstrings or comments to describe your work.
102+
- Original code submissions require docstrings or comments to describe your work.
103103

104104
- More on docstrings and comments:
105105

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@
624624
* [Sequential Minimum Optimization](machine_learning/sequential_minimum_optimization.py)
625625
* [Similarity Search](machine_learning/similarity_search.py)
626626
* [Support Vector Machines](machine_learning/support_vector_machines.py)
627+
* [T Stochastic Neighbour Embedding](machine_learning/t_stochastic_neighbour_embedding.py)
627628
* [Word Frequency Functions](machine_learning/word_frequency_functions.py)
628629
* [Xgboost Classifier](machine_learning/xgboost_classifier.py)
629630
* [Xgboost Regressor](machine_learning/xgboost_regressor.py)

data_structures/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Top-level data_structures package.
2+
3+
This file exposes a small `simple` collection of educational data structures
4+
under `data_structures.simple` and provides a few convenience imports.
5+
"""
6+
7+
from . import simple
8+
9+
__all__ = ["simple"]

data_structures/simple/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Simple implementations of common data structures.
2+
3+
This subpackage contains small, educational implementations copied from the
4+
`ds_extra` package so they can be accessed via `data_structures.simple`.
5+
"""
6+
7+
from .linked_list import LinkedList
8+
from .stack import ListStack, LinkedStack
9+
from .queue import Queue
10+
from .bst import BinarySearchTree
11+
from .graph import Graph
12+
from .trie import Trie
13+
from .union_find import UnionFind
14+
from .min_heap import MinHeap
15+
16+
__all__ = [
17+
"LinkedList",
18+
"ListStack",
19+
"LinkedStack",
20+
"Queue",
21+
"BinarySearchTree",
22+
"Graph",
23+
"Trie",
24+
"UnionFind",
25+
"MinHeap",
26+
]

data_structures/simple/bst.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import Any, Optional, List
2+
3+
4+
class BSTNode:
5+
def __init__(self, key: Any):
6+
self.key = key
7+
self.left: Optional[BSTNode] = None
8+
self.right: Optional[BSTNode] = None
9+
10+
11+
class BinarySearchTree:
12+
def __init__(self):
13+
self.root: Optional[BSTNode] = None
14+
15+
def insert(self, key: Any) -> None:
16+
def _insert(node: Optional[BSTNode], key: Any) -> BSTNode:
17+
if not node:
18+
return BSTNode(key)
19+
if key < node.key:
20+
node.left = _insert(node.left, key)
21+
else:
22+
node.right = _insert(node.right, key)
23+
return node
24+
25+
self.root = _insert(self.root, key)
26+
27+
def search(self, key: Any) -> bool:
28+
node = self.root
29+
while node:
30+
if key == node.key:
31+
return True
32+
node = node.left if key < node.key else node.right
33+
return False
34+
35+
def inorder(self) -> List[Any]:
36+
res: List[Any] = []
37+
38+
def _in(node: Optional[BSTNode]) -> None:
39+
if not node:
40+
return
41+
_in(node.left)
42+
res.append(node.key)
43+
_in(node.right)
44+
45+
_in(self.root)
46+
return res

data_structures/simple/graph.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from collections import defaultdict, deque
2+
from typing import Any, Dict, List
3+
4+
5+
class Graph:
6+
def __init__(self, directed: bool = False):
7+
self.adj: Dict[Any, List[Any]] = defaultdict(list)
8+
self.directed = directed
9+
10+
def add_edge(self, u: Any, v: Any) -> None:
11+
self.adj[u].append(v)
12+
if not self.directed:
13+
self.adj[v].append(u)
14+
15+
def bfs(self, start: Any) -> List[Any]:
16+
visited = set()
17+
order = []
18+
q = deque([start])
19+
visited.add(start)
20+
while q:
21+
u = q.popleft()
22+
order.append(u)
23+
for v in self.adj[u]:
24+
if v not in visited:
25+
visited.add(v)
26+
q.append(v)
27+
return order
28+
29+
def dfs(self, start: Any) -> List[Any]:
30+
visited = set()
31+
order = []
32+
33+
def _dfs(u: Any) -> None:
34+
visited.add(u)
35+
order.append(u)
36+
for v in self.adj[u]:
37+
if v not in visited:
38+
_dfs(v)
39+
40+
_dfs(start)
41+
return order
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from typing import Any, Optional
2+
3+
4+
class Node:
5+
def __init__(self, value: Any):
6+
self.value = value
7+
self.next: Optional[Node] = None
8+
9+
10+
class LinkedList:
11+
def __init__(self):
12+
self.head: Optional[Node] = None
13+
14+
def append(self, value: Any) -> None:
15+
node = Node(value)
16+
if not self.head:
17+
self.head = node
18+
return
19+
cur = self.head
20+
while cur.next:
21+
cur = cur.next
22+
cur.next = node
23+
24+
def prepend(self, value: Any) -> None:
25+
node = Node(value)
26+
node.next = self.head
27+
self.head = node
28+
29+
def find(self, value: Any) -> Optional[Node]:
30+
cur = self.head
31+
while cur:
32+
if cur.value == value:
33+
return cur
34+
cur = cur.next
35+
return None
36+
37+
def delete(self, value: Any) -> bool:
38+
cur = self.head
39+
prev = None
40+
while cur:
41+
if cur.value == value:
42+
if prev:
43+
prev.next = cur.next
44+
else:
45+
self.head = cur.next
46+
return True
47+
prev, cur = cur, cur.next
48+
return False
49+
50+
def to_list(self) -> list:
51+
out = []
52+
cur = self.head
53+
while cur:
54+
out.append(cur.value)
55+
cur = cur.next
56+
return out

data_structures/simple/min_heap.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import heapq
2+
from typing import Any, List, Optional
3+
4+
5+
class MinHeap:
6+
def __init__(self):
7+
self._data: List[Any] = []
8+
9+
def push(self, v: Any) -> None:
10+
heapq.heappush(self._data, v)
11+
12+
def pop(self) -> Any:
13+
return heapq.heappop(self._data)
14+
15+
def peek(self) -> Optional[Any]:
16+
return self._data[0] if self._data else None

data_structures/simple/queue.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from collections import deque
2+
from typing import Any, Optional
3+
4+
5+
class Queue:
6+
def __init__(self):
7+
self._dq = deque()
8+
9+
def enqueue(self, v: Any) -> None:
10+
self._dq.append(v)
11+
12+
def dequeue(self) -> Any:
13+
return self._dq.popleft()
14+
15+
def peek(self) -> Optional[Any]:
16+
return self._dq[0] if self._dq else None

0 commit comments

Comments
 (0)