Skip to content

Commit 49f60a8

Browse files
Merge branch 'master' into two_unique_numbers
2 parents a9a4e5d + e2a78d4 commit 49f60a8

File tree

16 files changed

+51
-46
lines changed

16 files changed

+51
-46
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/queues/circular_queue.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __len__(self) -> int:
1717
>>> len(cq)
1818
0
1919
>>> cq.enqueue("A") # doctest: +ELLIPSIS
20-
<data_structures.queues.circular_queue.CircularQueue object at ...
20+
<data_structures.queues.circular_queue.CircularQueue object at ...>
2121
>>> cq.array
2222
['A', None, None, None, None]
2323
>>> len(cq)
@@ -51,17 +51,24 @@ def enqueue(self, data):
5151
"""
5252
This function inserts an element at the end of the queue using self.rear value
5353
as an index.
54+
5455
>>> cq = CircularQueue(5)
5556
>>> cq.enqueue("A") # doctest: +ELLIPSIS
56-
<data_structures.queues.circular_queue.CircularQueue object at ...
57+
<data_structures.queues.circular_queue.CircularQueue object at ...>
5758
>>> (cq.size, cq.first())
5859
(1, 'A')
5960
>>> cq.enqueue("B") # doctest: +ELLIPSIS
60-
<data_structures.queues.circular_queue.CircularQueue object at ...
61+
<data_structures.queues.circular_queue.CircularQueue object at ...>
6162
>>> cq.array
6263
['A', 'B', None, None, None]
6364
>>> (cq.size, cq.first())
6465
(2, 'A')
66+
>>> cq.enqueue("C").enqueue("D").enqueue("E") # doctest: +ELLIPSIS
67+
<data_structures.queues.circular_queue.CircularQueue object at ...>
68+
>>> cq.enqueue("F")
69+
Traceback (most recent call last):
70+
...
71+
Exception: QUEUE IS FULL
6572
"""
6673
if self.size >= self.n:
6774
raise Exception("QUEUE IS FULL")
@@ -75,6 +82,7 @@ def dequeue(self):
7582
"""
7683
This function removes an element from the queue using on self.front value as an
7784
index and returns it
85+
7886
>>> cq = CircularQueue(5)
7987
>>> cq.dequeue()
8088
Traceback (most recent call last):

graphs/graph_adjacency_list.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ def add_vertex(self, vertex: T) -> None:
6161
"""
6262
Adds a vertex to the graph. If the given vertex already exists,
6363
a ValueError will be thrown.
64+
65+
>>> g = GraphAdjacencyList(vertices=[], edges=[], directed=False)
66+
>>> g.add_vertex("A")
67+
>>> g.adj_list
68+
{'A': []}
69+
>>> g.add_vertex("A")
70+
Traceback (most recent call last):
71+
...
72+
ValueError: Incorrect input: A is already in the graph.
6473
"""
6574
if self.contains_vertex(vertex):
6675
msg = f"Incorrect input: {vertex} is already in the graph."

machine_learning/apriori_algorithm.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Examples: https://www.kaggle.com/code/earthian/apriori-association-rules-mining
1212
"""
1313

14+
from collections import Counter
1415
from itertools import combinations
1516

1617

@@ -44,11 +45,16 @@ def prune(itemset: list, candidates: list, length: int) -> list:
4445
>>> prune(itemset, candidates, 3)
4546
[]
4647
"""
48+
itemset_counter = Counter(tuple(item) for item in itemset)
4749
pruned = []
4850
for candidate in candidates:
4951
is_subsequence = True
5052
for item in candidate:
51-
if item not in itemset or itemset.count(item) < length - 1:
53+
item_tuple = tuple(item)
54+
if (
55+
item_tuple not in itemset_counter
56+
or itemset_counter[item_tuple] < length - 1
57+
):
5258
is_subsequence = False
5359
break
5460
if is_subsequence:

machine_learning/decision_tree.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,13 @@ def predict(self, x):
146146
"""
147147
if self.prediction is not None:
148148
return self.prediction
149-
elif self.left or self.right is not None:
149+
elif self.left is not None and self.right is not None:
150150
if x >= self.decision_boundary:
151151
return self.right.predict(x)
152152
else:
153153
return self.left.predict(x)
154154
else:
155-
print("Error: Decision tree not yet trained")
156-
return None
155+
raise ValueError("Decision tree not yet trained")
157156

158157

159158
class TestDecisionTree:
@@ -201,4 +200,4 @@ def main():
201200
main()
202201
import doctest
203202

204-
doctest.testmod(name="mean_squarred_error", verbose=True)
203+
doctest.testmod(name="mean_squared_error", verbose=True)

maths/factorial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def factorial_recursive(n: int) -> int:
5656
raise ValueError("factorial() only accepts integral values")
5757
if n < 0:
5858
raise ValueError("factorial() not defined for negative values")
59-
return 1 if n in {0, 1} else n * factorial(n - 1)
59+
return 1 if n in {0, 1} else n * factorial_recursive(n - 1)
6060

6161

6262
if __name__ == "__main__":

maths/fibonacci.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def fib_memoization(n: int) -> list[int]:
183183
"""
184184
if n < 0:
185185
raise ValueError("n is negative")
186-
# Cache must be outside recursuive function
186+
# Cache must be outside recursive function
187187
# other it will reset every time it calls itself.
188188
cache: dict[int, int] = {0: 0, 1: 1, 2: 1} # Prefilled cache
189189

maths/monte_carlo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from statistics import mean
99

1010

11-
def pi_estimator(iterations: int):
11+
def pi_estimator(iterations: int) -> None:
1212
"""
1313
An implementation of the Monte Carlo method used to find pi.
1414
1. Draw a 2x2 square centred at (0,0).

0 commit comments

Comments
 (0)