Skip to content

Commit e27c028

Browse files
Merge branch 'TheAlgorithms:master' into master
2 parents f1387e7 + 4fe50bc commit e27c028

29 files changed

+138
-102
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
- id: auto-walrus
1717

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

DIRECTORY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
* [Baconian Cipher](ciphers/baconian_cipher.py)
8787
* [Base16](ciphers/base16.py)
8888
* [Base32](ciphers/base32.py)
89-
* [Base64](ciphers/base64.py)
89+
* [Base64 Cipher](ciphers/base64_cipher.py)
9090
* [Base85](ciphers/base85.py)
9191
* [Beaufort Cipher](ciphers/beaufort_cipher.py)
9292
* [Bifid](ciphers/bifid.py)
@@ -1331,7 +1331,7 @@
13311331
* [Title](strings/title.py)
13321332
* [Top K Frequent Words](strings/top_k_frequent_words.py)
13331333
* [Upper](strings/upper.py)
1334-
* [Wave](strings/wave.py)
1334+
* [Wave String](strings/wave_string.py)
13351335
* [Wildcard Pattern Matching](strings/wildcard_pattern_matching.py)
13361336
* [Word Occurrence](strings/word_occurrence.py)
13371337
* [Word Patterns](strings/word_patterns.py)

backtracking/all_combinations.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
def combination_lists(n: int, k: int) -> list[list[int]]:
1414
"""
15+
Generates all possible combinations of k numbers out of 1 ... n using itertools.
16+
1517
>>> combination_lists(n=4, k=2)
1618
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
1719
"""
@@ -20,6 +22,8 @@ def combination_lists(n: int, k: int) -> list[list[int]]:
2022

2123
def generate_all_combinations(n: int, k: int) -> list[list[int]]:
2224
"""
25+
Generates all possible combinations of k numbers out of 1 ... n using backtracking.
26+
2327
>>> generate_all_combinations(n=4, k=2)
2428
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
2529
>>> generate_all_combinations(n=0, k=0)
@@ -34,6 +38,14 @@ def generate_all_combinations(n: int, k: int) -> list[list[int]]:
3438
ValueError: n must not be negative
3539
>>> generate_all_combinations(n=5, k=4)
3640
[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]]
41+
>>> generate_all_combinations(n=3, k=3)
42+
[[1, 2, 3]]
43+
>>> generate_all_combinations(n=3, k=1)
44+
[[1], [2], [3]]
45+
>>> generate_all_combinations(n=1, k=0)
46+
[[]]
47+
>>> generate_all_combinations(n=1, k=1)
48+
[[1]]
3749
>>> from itertools import combinations
3850
>>> all(generate_all_combinations(n, k) == combination_lists(n, k)
3951
... for n in range(1, 6) for k in range(1, 6))
@@ -56,6 +68,28 @@ def create_all_state(
5668
current_list: list[int],
5769
total_list: list[list[int]],
5870
) -> None:
71+
"""
72+
Helper function to recursively build all combinations.
73+
74+
>>> create_all_state(1, 4, 2, [], result := [])
75+
>>> result
76+
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
77+
>>> create_all_state(1, 3, 3, [], result := [])
78+
>>> result
79+
[[1, 2, 3]]
80+
>>> create_all_state(2, 2, 1, [1], result := [])
81+
>>> result
82+
[[1, 2]]
83+
>>> create_all_state(1, 0, 0, [], result := [])
84+
>>> result
85+
[[]]
86+
>>> create_all_state(1, 4, 0, [1, 2], result := [])
87+
>>> result
88+
[[1, 2]]
89+
>>> create_all_state(5, 4, 2, [1, 2], result := [])
90+
>>> result
91+
[]
92+
"""
5993
if level == 0:
6094
total_list.append(current_list[:])
6195
return

ciphers/base64.py renamed to ciphers/base64_cipher.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ def base64_decode(encoded_data: str) -> bytes:
105105

106106
# Check if the encoded string contains non base64 characters
107107
if padding:
108-
assert all(
109-
char in B64_CHARSET for char in encoded_data[:-padding]
110-
), "Invalid base64 character(s) found."
108+
assert all(char in B64_CHARSET for char in encoded_data[:-padding]), (
109+
"Invalid base64 character(s) found."
110+
)
111111
else:
112-
assert all(
113-
char in B64_CHARSET for char in encoded_data
114-
), "Invalid base64 character(s) found."
112+
assert all(char in B64_CHARSET for char in encoded_data), (
113+
"Invalid base64 character(s) found."
114+
)
115115

116116
# Check the padding
117117
assert len(encoded_data) % 4 == 0 and padding < 3, "Incorrect padding"

ciphers/caesar_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str
225225

226226
if __name__ == "__main__":
227227
while True:
228-
print(f'\n{"-" * 10}\n Menu\n{"-" * 10}')
228+
print(f"\n{'-' * 10}\n Menu\n{'-' * 10}")
229229
print(*["1.Encrypt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep="\n")
230230

231231
# get user input

computer_vision/flip_augmentation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def main() -> None:
3333
file_name = paths[index].split(os.sep)[-1].rsplit(".", 1)[0]
3434
file_root = f"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}"
3535
cv2.imwrite(f"{file_root}.jpg", image, [cv2.IMWRITE_JPEG_QUALITY, 85])
36-
print(f"Success {index+1}/{len(new_images)} with {file_name}")
36+
print(f"Success {index + 1}/{len(new_images)} with {file_name}")
3737
annos_list = []
3838
for anno in new_annos[index]:
3939
obj = f"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}"

computer_vision/mosaic_augmentation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def main() -> None:
4141
file_name = path.split(os.sep)[-1].rsplit(".", 1)[0]
4242
file_root = f"{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}"
4343
cv2.imwrite(f"{file_root}.jpg", new_image, [cv2.IMWRITE_JPEG_QUALITY, 85])
44-
print(f"Succeeded {index+1}/{NUMBER_IMAGES} with {file_name}")
44+
print(f"Succeeded {index + 1}/{NUMBER_IMAGES} with {file_name}")
4545
annos_list = []
4646
for anno in new_annos:
4747
width = anno[3] - anno[1]

data_structures/hashing/number_theory/prime_numbers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def is_prime(number: int) -> bool:
3232
"""
3333

3434
# precondition
35-
assert isinstance(number, int) and (
36-
number >= 0
37-
), "'number' must been an int and positive"
35+
assert isinstance(number, int) and (number >= 0), (
36+
"'number' must been an int and positive"
37+
)
3838

3939
if 1 < number < 4:
4040
# 2 and 3 are primes

data_structures/heap/min_heap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ def is_empty(self):
124124
return len(self.heap) == 0
125125

126126
def decrease_key(self, node, new_value):
127-
assert (
128-
self.heap[self.idx_of_element[node]].val > new_value
129-
), "newValue must be less that current value"
127+
assert self.heap[self.idx_of_element[node]].val > new_value, (
128+
"newValue must be less that current value"
129+
)
130130
node.val = new_value
131131
self.heap_dict[node.name] = new_value
132132
self.sift_up(self.idx_of_element[node])

data_structures/kd_tree/tests/test_kdtree.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ def test_build_kdtree(num_points, cube_size, num_dimensions, depth, expected_res
4848
assert kdtree is not None, "Expected a KDNode, got None"
4949

5050
# Check if root has correct dimensions
51-
assert (
52-
len(kdtree.point) == num_dimensions
53-
), f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}"
51+
assert len(kdtree.point) == num_dimensions, (
52+
f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}"
53+
)
5454

5555
# Check that the tree is balanced to some extent (simplistic check)
56-
assert isinstance(
57-
kdtree, KDNode
58-
), f"Expected KDNode instance, got {type(kdtree)}"
56+
assert isinstance(kdtree, KDNode), (
57+
f"Expected KDNode instance, got {type(kdtree)}"
58+
)
5959

6060

6161
def test_nearest_neighbour_search():

0 commit comments

Comments
 (0)