Skip to content

Commit 8f32300

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents a93cd95 + a73f37b commit 8f32300

13 files changed

+780
-30
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ repos:
1616
- id: auto-walrus
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.1.4
19+
rev: v0.1.6
2020
hooks:
2121
- id: ruff
2222

2323
- repo: https://github.com/psf/black
24-
rev: 23.10.1
24+
rev: 23.11.0
2525
hooks:
2626
- id: black
2727

@@ -33,7 +33,7 @@ repos:
3333
- tomli
3434

3535
- repo: https://github.com/tox-dev/pyproject-fmt
36-
rev: "1.4.1"
36+
rev: "1.5.3"
3737
hooks:
3838
- id: pyproject-fmt
3939

@@ -51,7 +51,7 @@ repos:
5151
- id: validate-pyproject
5252

5353
- repo: https://github.com/pre-commit/mirrors-mypy
54-
rev: v1.6.1
54+
rev: v1.7.1
5555
hooks:
5656
- id: mypy
5757
args:

DIRECTORY.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,16 @@
428428
* [Haversine Distance](geodesy/haversine_distance.py)
429429
* [Lamberts Ellipsoidal Distance](geodesy/lamberts_ellipsoidal_distance.py)
430430

431+
## Geometry
432+
* [Geometry](geometry/geometry.py)
433+
431434
## Graphics
432435
* [Bezier Curve](graphics/bezier_curve.py)
433436
* [Vector3 For 2D Rendering](graphics/vector3_for_2d_rendering.py)
434437

435438
## Graphs
436439
* [A Star](graphs/a_star.py)
440+
* [Ant Colony Optimization Algorithms](graphs/ant_colony_optimization_algorithms.py)
437441
* [Articulation Points](graphs/articulation_points.py)
438442
* [Basic Graphs](graphs/basic_graphs.py)
439443
* [Bellman Ford](graphs/bellman_ford.py)
@@ -503,6 +507,7 @@
503507
* [Minimum Coin Change](greedy_methods/minimum_coin_change.py)
504508
* [Minimum Waiting Time](greedy_methods/minimum_waiting_time.py)
505509
* [Optimal Merge Pattern](greedy_methods/optimal_merge_pattern.py)
510+
* [Smallest Range](greedy_methods/smallest_range.py)
506511

507512
## Hashes
508513
* [Adler32](hashes/adler32.py)
@@ -718,6 +723,7 @@
718723
* [Sock Merchant](maths/sock_merchant.py)
719724
* [Softmax](maths/softmax.py)
720725
* [Solovay Strassen Primality Test](maths/solovay_strassen_primality_test.py)
726+
* [Spearman Rank Correlation Coefficient](maths/spearman_rank_correlation_coefficient.py)
721727
* Special Numbers
722728
* [Armstrong Numbers](maths/special_numbers/armstrong_numbers.py)
723729
* [Automorphic Number](maths/special_numbers/automorphic_number.py)
@@ -1310,7 +1316,6 @@
13101316
* [Fetch Well Rx Price](web_programming/fetch_well_rx_price.py)
13111317
* [Get Amazon Product Data](web_programming/get_amazon_product_data.py)
13121318
* [Get Imdb Top 250 Movies Csv](web_programming/get_imdb_top_250_movies_csv.py)
1313-
* [Get Imdbtop](web_programming/get_imdbtop.py)
13141319
* [Get Ip Geolocation](web_programming/get_ip_geolocation.py)
13151320
* [Get Top Billionaires](web_programming/get_top_billionaires.py)
13161321
* [Get Top Hn Posts](web_programming/get_top_hn_posts.py)

backtracking/all_combinations.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@ def generate_all_combinations(n: int, k: int) -> list[list[int]]:
2626
>>> generate_all_combinations(n=10, k=-1)
2727
Traceback (most recent call last):
2828
...
29-
RecursionError: maximum recursion depth exceeded
29+
ValueError: k must not be negative
3030
>>> generate_all_combinations(n=-1, k=10)
31-
[]
31+
Traceback (most recent call last):
32+
...
33+
ValueError: n must not be negative
3234
>>> generate_all_combinations(n=5, k=4)
3335
[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]]
3436
>>> from itertools import combinations
3537
>>> all(generate_all_combinations(n, k) == combination_lists(n, k)
3638
... for n in range(1, 6) for k in range(1, 6))
3739
True
3840
"""
41+
if k < 0:
42+
raise ValueError("k must not be negative")
43+
if n < 0:
44+
raise ValueError("n must not be negative")
3945

4046
result: list[list[int]] = []
4147
create_all_state(1, n, k, [], result)

backtracking/knight_tour.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def open_knight_tour(n: int) -> list[list[int]]:
7979
>>> open_knight_tour(2)
8080
Traceback (most recent call last):
8181
...
82-
ValueError: Open Kight Tour cannot be performed on a board of size 2
82+
ValueError: Open Knight Tour cannot be performed on a board of size 2
8383
"""
8484

8585
board = [[0 for i in range(n)] for j in range(n)]
@@ -91,7 +91,7 @@ def open_knight_tour(n: int) -> list[list[int]]:
9191
return board
9292
board[i][j] = 0
9393

94-
msg = f"Open Kight Tour cannot be performed on a board of size {n}"
94+
msg = f"Open Knight Tour cannot be performed on a board of size {n}"
9595
raise ValueError(msg)
9696

9797

backtracking/n_queens.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def is_safe(board: list[list[int]], row: int, column: int) -> bool:
2424
Returns:
2525
Boolean Value
2626
27+
>>> is_safe([[0, 0, 0], [0, 0, 0], [0, 0, 0]], 1, 1)
28+
True
29+
>>> is_safe([[1, 0, 0], [0, 0, 0], [0, 0, 0]], 1, 1)
30+
False
2731
"""
2832

2933
n = len(board) # Size of the board

bit_manipulation/is_even.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
def is_even(number: int) -> bool:
22
"""
33
return true if the input integer is even
4-
Explanation: Lets take a look at the following deicmal to binary conversions
4+
Explanation: Lets take a look at the following decimal to binary conversions
55
2 => 10
66
14 => 1110
77
100 => 1100100

data_structures/binary_tree/binary_search_tree.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,30 @@ def insert(self, *values) -> Self:
198198
return self
199199

200200
def search(self, value) -> Node | None:
201+
"""
202+
>>> tree = BinarySearchTree().insert(10, 20, 30, 40, 50)
203+
>>> tree.search(10)
204+
{'10': (None, {'20': (None, {'30': (None, {'40': (None, 50)})})})}
205+
>>> tree.search(20)
206+
{'20': (None, {'30': (None, {'40': (None, 50)})})}
207+
>>> tree.search(30)
208+
{'30': (None, {'40': (None, 50)})}
209+
>>> tree.search(40)
210+
{'40': (None, 50)}
211+
>>> tree.search(50)
212+
50
213+
>>> tree.search(5) is None # element not present
214+
True
215+
>>> tree.search(0) is None # element not present
216+
True
217+
>>> tree.search(-5) is None # element not present
218+
True
219+
>>> BinarySearchTree().search(10)
220+
Traceback (most recent call last):
221+
...
222+
IndexError: Warning: Tree is empty! please use another.
223+
"""
224+
201225
if self.empty():
202226
raise IndexError("Warning: Tree is empty! please use another.")
203227
else:
@@ -210,6 +234,15 @@ def search(self, value) -> Node | None:
210234
def get_max(self, node: Node | None = None) -> Node | None:
211235
"""
212236
We go deep on the right branch
237+
238+
>>> BinarySearchTree().insert(10, 20, 30, 40, 50).get_max()
239+
50
240+
>>> BinarySearchTree().insert(-5, -1, 0.1, -0.3, -4.5).get_max()
241+
{'0.1': (-0.3, None)}
242+
>>> BinarySearchTree().insert(1, 78.3, 30, 74.0, 1).get_max()
243+
{'78.3': ({'30': (1, 74.0)}, None)}
244+
>>> BinarySearchTree().insert(1, 783, 30, 740, 1).get_max()
245+
{'783': ({'30': (1, 740)}, None)}
213246
"""
214247
if node is None:
215248
if self.root is None:
@@ -224,6 +257,15 @@ def get_max(self, node: Node | None = None) -> Node | None:
224257
def get_min(self, node: Node | None = None) -> Node | None:
225258
"""
226259
We go deep on the left branch
260+
261+
>>> BinarySearchTree().insert(10, 20, 30, 40, 50).get_min()
262+
{'10': (None, {'20': (None, {'30': (None, {'40': (None, 50)})})})}
263+
>>> BinarySearchTree().insert(-5, -1, 0, -0.3, -4.5).get_min()
264+
{'-5': (None, {'-1': (-4.5, {'0': (-0.3, None)})})}
265+
>>> BinarySearchTree().insert(1, 78.3, 30, 74.0, 1).get_min()
266+
{'1': (None, {'78.3': ({'30': (1, 74.0)}, None)})}
267+
>>> BinarySearchTree().insert(1, 783, 30, 740, 1).get_min()
268+
{'1': (None, {'783': ({'30': (1, 740)}, None)})}
227269
"""
228270
if node is None:
229271
node = self.root

0 commit comments

Comments
 (0)