Skip to content

Commit eecb83e

Browse files
authored
Merge branch 'master' into fix-14970-bucket-sort-float
2 parents c085b69 + d86f4fe commit eecb83e

228 files changed

Lines changed: 3062 additions & 824 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ repos:
3434
- id: pyproject-fmt
3535

3636
- repo: https://github.com/astral-sh/ruff-pre-commit
37-
rev: v0.16.6
37+
rev: v0.16.7
3838
hooks:
3939
- id: ruff-check
4040
- id: ruff-format
4141

4242
- repo: https://github.com/rvben/rumdl-pre-commit
43-
rev: v0.2.69
43+
rev: v0.2.73
4444
hooks:
4545
- id: rumdl-fmt
4646
- id: rumdl
@@ -55,7 +55,7 @@ repos:
5555
pass_filenames: false
5656

5757
- repo: https://github.com/astral-sh/uv-pre-commit
58-
rev: 0.12.11
58+
rev: 0.12.13
5959
hooks:
6060
- id: uv-lock
6161

DIRECTORY.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,14 @@
181181
## [Computer Vision](computer_vision)
182182
* [Cnn Classification](computer_vision/cnn_classification.py)
183183
* [Flip Augmentation](computer_vision/flip_augmentation.py)
184+
* [Gramian](computer_vision/gramian.py)
184185
* [Haralick Descriptors](computer_vision/haralick_descriptors.py)
185186
* [Harris Corner](computer_vision/harris_corner.py)
186187
* [Horn Schunck](computer_vision/horn_schunck.py)
187188
* [Intensity Based Segmentation](computer_vision/intensity_based_segmentation.py)
188189
* [Mean Threshold](computer_vision/mean_threshold.py)
189190
* [Mosaic Augmentation](computer_vision/mosaic_augmentation.py)
191+
* [Otsu Threshold](computer_vision/otsu_threshold.py)
190192
* [Pooling Functions](computer_vision/pooling_functions.py)
191193
* [Vision Transformer](computer_vision/vision_transformer.py)
192194

@@ -230,6 +232,7 @@
230232
* [Lempel Ziv](data_compression/lempel_ziv.py)
231233
* [Lempel Ziv Decompress](data_compression/lempel_ziv_decompress.py)
232234
* [Lz77](data_compression/lz77.py)
235+
* [Move To Front](data_compression/move_to_front.py)
233236
* [Peak Signal To Noise Ratio](data_compression/peak_signal_to_noise_ratio.py)
234237
* [Run Length Encoding](data_compression/run_length_encoding.py)
235238

@@ -323,16 +326,19 @@
323326
* [From Sequence](data_structures/linked_list/from_sequence.py)
324327
* [Has Loop](data_structures/linked_list/has_loop.py)
325328
* [Is Palindrome](data_structures/linked_list/is_palindrome.py)
329+
* [Kth Element From End](data_structures/linked_list/kth_element_from_end.py)
326330
* [Merge Sort Linked List](data_structures/linked_list/merge_sort_linked_list.py)
327331
* [Merge Two Lists](data_structures/linked_list/merge_two_lists.py)
328332
* [Middle Element Of Linked List](data_structures/linked_list/middle_element_of_linked_list.py)
333+
* [Partition Linked List](data_structures/linked_list/partition_linked_list.py)
329334
* [Print Reverse](data_structures/linked_list/print_reverse.py)
330335
* [Reverse K Group](data_structures/linked_list/reverse_k_group.py)
331336
* [Rotate To The Right](data_structures/linked_list/rotate_to_the_right.py)
332337
* [Singly Linked List](data_structures/linked_list/singly_linked_list.py)
333338
* [Skip List](data_structures/linked_list/skip_list.py)
334339
* [Sorted Linked List](data_structures/linked_list/sorted_linked_list.py)
335340
* [Swap Nodes](data_structures/linked_list/swap_nodes.py)
341+
* [Xor Linked List](data_structures/linked_list/xor_linked_list.py)
336342
* Queues
337343
* [Circular Queue](data_structures/queues/circular_queue.py)
338344
* [Circular Queue Linked List](data_structures/queues/circular_queue_linked_list.py)
@@ -424,6 +430,7 @@
424430
* [Climbing Stairs](dynamic_programming/climbing_stairs.py)
425431
* [Combination Sum Iv](dynamic_programming/combination_sum_iv.py)
426432
* [Edit Distance](dynamic_programming/edit_distance.py)
433+
* [Egg Dropping](dynamic_programming/egg_dropping.py)
427434
* [Factorial](dynamic_programming/factorial.py)
428435
* [Fast Fibonacci](dynamic_programming/fast_fibonacci.py)
429436
* [Fibonacci](dynamic_programming/fibonacci.py)
@@ -454,6 +461,7 @@
454461
* [Minimum Steps To One](dynamic_programming/minimum_steps_to_one.py)
455462
* [Minimum Tickets Cost](dynamic_programming/minimum_tickets_cost.py)
456463
* [Narcissistic Number](dynamic_programming/narcissistic_number.py)
464+
* [Needleman Wunsch](dynamic_programming/needleman_wunsch.py)
457465
* [Optimal Binary Search Tree](dynamic_programming/optimal_binary_search_tree.py)
458466
* [Palindrome Partitioning](dynamic_programming/palindrome_partitioning.py)
459467
* [Range Sum Query](dynamic_programming/range_sum_query.py)
@@ -540,6 +548,7 @@
540548
* Tests
541549
* [Test Graham Scan](geometry/tests/test_graham_scan.py)
542550
* [Test Jarvis March](geometry/tests/test_jarvis_march.py)
551+
* [Triangle](geometry/triangle.py)
543552

544553
## [Graphics](graphics)
545554
* [Bezier Curve](graphics/bezier_curve.py)
@@ -589,6 +598,7 @@
589598
* [Graphs Floyd Warshall](graphs/graphs_floyd_warshall.py)
590599
* [Greedy Best First](graphs/greedy_best_first.py)
591600
* [Greedy Min Vertex Cover](graphs/greedy_min_vertex_cover.py)
601+
* [Hopcroft Karp](graphs/hopcroft_karp.py)
592602
* [Johnson](graphs/johnson.py)
593603
* [Kahns Algorithm Long](graphs/kahns_algorithm_long.py)
594604
* [Kahns Algorithm Topo](graphs/kahns_algorithm_topo.py)
@@ -613,6 +623,7 @@
613623
* [Test Johnson](graphs/tests/test_johnson.py)
614624
* [Test Min Spanning Tree Kruskal](graphs/tests/test_min_spanning_tree_kruskal.py)
615625
* [Test Min Spanning Tree Prim](graphs/tests/test_min_spanning_tree_prim.py)
626+
* [Travelling Salesman Problem](graphs/travelling_salesman_problem.py)
616627

617628
## [Greedy Methods](greedy_methods)
618629
* [Best Time To Buy And Sell Stock](greedy_methods/best_time_to_buy_and_sell_stock.py)
@@ -648,6 +659,7 @@
648659
* [Test Knapsack](knapsack/tests/test_knapsack.py)
649660

650661
## [Linear Algebra](linear_algebra)
662+
* [Gauss Jordan](linear_algebra/gauss_jordan.py)
651663
* [Gaussian Elimination](linear_algebra/gaussian_elimination.py)
652664
* [Jacobi Iteration Method](linear_algebra/jacobi_iteration_method.py)
653665
* [Lu Decomposition](linear_algebra/lu_decomposition.py)
@@ -1372,6 +1384,7 @@
13721384
## [Sorts](sorts)
13731385
* [Adaptive Merge Sort](sorts/adaptive_merge_sort.py)
13741386
* [Bead Sort](sorts/bead_sort.py)
1387+
* [Benchmark Sorts](sorts/benchmark_sorts.py)
13751388
* [Binary Insertion Sort](sorts/binary_insertion_sort.py)
13761389
* [Bitonic Sort](sorts/bitonic_sort.py)
13771390
* [Bogo Sort](sorts/bogo_sort.py)

bit_manipulation/binary_count_setbits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def binary_count_setbits(a: int) -> int:
3030
"""
3131
if a < 0:
3232
raise ValueError("Input value must be a positive integer")
33-
elif isinstance(a, float):
33+
if isinstance(a, float):
3434
raise TypeError("Input value must be a 'int' type")
3535
return bin(a).count("1")
3636

bit_manipulation/binary_count_trailing_zeros.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def binary_count_trailing_zeros(a: int) -> int:
3333
"""
3434
if a < 0:
3535
raise ValueError("Input value must be a positive integer")
36-
elif isinstance(a, float):
36+
if isinstance(a, float):
3737
raise TypeError("Input value must be a 'int' type")
3838
return 0 if (a == 0) else int(log2(a & -a))
3939

bit_manipulation/single_bit_manipulation_operations.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@ def get_bit(number: int, position: int) -> int:
9494
return int((number & (1 << position)) != 0)
9595

9696

97+
def clear_least_significant_set_bit(number: int) -> int:
98+
"""
99+
Clear the least significant set bit (rightmost 1 bit).
100+
101+
Subtracting 1 changes the rightmost 1 to 0 and the 0 bits to its right to 1.
102+
ANDing the result with the original number therefore clears that set bit.
103+
For negative integers, Python's infinite sign extension is used.
104+
https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
105+
106+
>>> clear_least_significant_set_bit(0b101100) # 0b101000
107+
40
108+
>>> clear_least_significant_set_bit(0b1000) # 0b0
109+
0
110+
>>> clear_least_significant_set_bit(0)
111+
0
112+
>>> clear_least_significant_set_bit(0b1111) # 0b1110
113+
14
114+
>>> clear_least_significant_set_bit(-5)
115+
-6
116+
"""
117+
return number & (number - 1)
118+
119+
97120
if __name__ == "__main__":
98121
import doctest
99122

ciphers/enigma_machine2.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ def _plugboard(pbstring: str) -> dict[str, str]:
134134
if not isinstance(pbstring, str):
135135
msg = f"Plugboard setting isn't type string ({type(pbstring)})"
136136
raise TypeError(msg)
137-
elif len(pbstring) % 2 != 0:
137+
if len(pbstring) % 2 != 0:
138138
msg = f"Odd number of symbols ({len(pbstring)})"
139139
raise Exception(msg)
140-
elif pbstring == "":
140+
if pbstring == "":
141141
return {}
142142

143143
pbstring.replace(" ", "")
@@ -148,11 +148,10 @@ def _plugboard(pbstring: str) -> dict[str, str]:
148148
if i not in abc:
149149
msg = f"'{i}' not in list of symbols"
150150
raise Exception(msg)
151-
elif i in tmppbl:
151+
if i in tmppbl:
152152
msg = f"Duplicate symbol ({i})"
153153
raise Exception(msg)
154-
else:
155-
tmppbl.add(i)
154+
tmppbl.add(i)
156155
del tmppbl
157156

158157
# Created the dictionary

ciphers/simple_substitution_cipher.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ def main() -> None:
2222

2323

2424
def check_valid_key(key: str) -> None:
25+
"""
26+
Check if the key is valid (contains all 26 letters of the alphabet exactly once).
27+
Exits the program if the key is invalid.
28+
29+
>>> check_valid_key('LFWOAYUISVKMNXPBDCRJTQEGHZ')
30+
>>> check_valid_key('INVALIDKEY')
31+
Traceback (most recent call last):
32+
...
33+
SystemExit: Error in the key or symbol set.
34+
"""
2535
key_list = list(key)
2636
letters_list = list(LETTERS)
2737
key_list.sort()
@@ -69,6 +79,13 @@ def translate_message(key: str, message: str, mode: str) -> str:
6979

7080

7181
def get_random_key() -> str:
82+
"""
83+
Generate a random substitution cipher key.
84+
85+
>>> random.seed(0)
86+
>>> get_random_key()
87+
'OAXSGFHKWUECVDRLTJZPQIBNYM'
88+
"""
7289
key = list(LETTERS)
7390
random.shuffle(key)
7491
return "".join(key)

ciphers/xor_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
class XORCipher:
24-
def __init__(self, key: int = 0):
24+
def __init__(self, key: int = 0) -> None:
2525
"""
2626
simple constructor that receives a key or uses
2727
default key = 0

computer_vision/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Computer Vision
22

3-
Computer vision is a field of computer science that works on enabling computers to see, identify and process images in the same way that human does, and provide appropriate output.
3+
Computer vision is an interdisciplinary field focused on enabling computers to gain high-level understanding from images and video—automatically extracting, analyzing, and interpreting visual information to produce outputs such as labels, measurements, 3D structure, or decisions.
44

5-
It is like imparting human intelligence and instincts to a computer.
6-
Image processing and computer vision are a little different from each other. Image processing means applying some algorithms for transforming image from one form to the other like smoothing, contrasting, stretching, etc.
5+
In practice, computer vision methods combine geometry, physics, statistics, and machine learning to connect pixel data to semantic concepts like objects, actions, and scenes.
76

8-
While computer vision comes from modelling image processing using the techniques of machine learning, computer vision applies machine learning to recognize patterns for interpretation of images (much like the process of visual reasoning of human vision).
7+
## Image processing vs. computer vision
98

10-
* <https://en.wikipedia.org/wiki/Computer_vision>
9+
Image processing primarily transforms images (e.g., denoising, contrast enhancement, geometric warping) where the output is another image.
10+
11+
Computer vision uses images/video as input but often outputs information about the scene (e.g., detections, segmentation masks, pose estimates, tracking results, or a decision), which may then drive downstream behavior in a larger system.

computer_vision/cnn_classification.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
test_image = np.expand_dims(test_image, axis=0)
9595
result = classifier.predict(test_image)
9696
# training_set.class_indices
97-
if result[0][0] == 0:
98-
prediction = "Normal"
99-
if result[0][0] == 1:
100-
prediction = "Abnormality detected"
97+
# The sigmoid output is a probability in the range [0, 1].
98+
# Use a threshold of 0.5 to convert the probability into a binary prediction.
99+
prediction = "Normal" if result[0][0] < 0.5 else "Abnormality detected"

0 commit comments

Comments
 (0)