Skip to content

Commit 26af407

Browse files
authored
Merge branch 'TheAlgorithms:master' into project_euler_seboll
2 parents 029ec46 + ed1900f commit 26af407

File tree

213 files changed

+1143
-742
lines changed

Some content is hidden

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

213 files changed

+1143
-742
lines changed

.pre-commit-config.yaml

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.5.0
3+
rev: v4.6.0
44
hooks:
55
- id: check-executables-have-shebangs
66
- id: check-toml
@@ -11,25 +11,25 @@ repos:
1111
- id: requirements-txt-fixer
1212

1313
- repo: https://github.com/MarcoGorelli/auto-walrus
14-
rev: v0.2.2
14+
rev: 0.3.4
1515
hooks:
1616
- id: auto-walrus
1717

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

2424
- repo: https://github.com/codespell-project/codespell
25-
rev: v2.2.6
25+
rev: v2.3.0
2626
hooks:
2727
- id: codespell
2828
additional_dependencies:
2929
- tomli
3030

3131
- repo: https://github.com/tox-dev/pyproject-fmt
32-
rev: "1.7.0"
32+
rev: "2.2.1"
3333
hooks:
3434
- id: pyproject-fmt
3535

@@ -42,15 +42,16 @@ repos:
4242
pass_filenames: false
4343

4444
- repo: https://github.com/abravalheri/validate-pyproject
45-
rev: v0.16
45+
rev: v0.18
4646
hooks:
4747
- id: validate-pyproject
4848

4949
- repo: https://github.com/pre-commit/mirrors-mypy
50-
rev: v1.9.0
50+
rev: v1.11.1
5151
hooks:
5252
- id: mypy
5353
args:
54+
- --explicit-package-bases
5455
- --ignore-missing-imports
5556
- --install-types # See mirrors-mypy README.md
5657
- --non-interactive

DIRECTORY.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,6 @@
661661
* [Manhattan Distance](maths/manhattan_distance.py)
662662
* [Matrix Exponentiation](maths/matrix_exponentiation.py)
663663
* [Max Sum Sliding Window](maths/max_sum_sliding_window.py)
664-
* [Median Of Two Arrays](maths/median_of_two_arrays.py)
665664
* [Minkowski Distance](maths/minkowski_distance.py)
666665
* [Mobius Function](maths/mobius_function.py)
667666
* [Modular Division](maths/modular_division.py)
@@ -773,6 +772,7 @@
773772
* [Inverse Of Matrix](matrix/inverse_of_matrix.py)
774773
* [Largest Square Area In Matrix](matrix/largest_square_area_in_matrix.py)
775774
* [Matrix Class](matrix/matrix_class.py)
775+
* [Matrix Equalization](matrix/matrix_equalization.py)
776776
* [Matrix Multiplication Recursion](matrix/matrix_multiplication_recursion.py)
777777
* [Matrix Operation](matrix/matrix_operation.py)
778778
* [Max Area Of Island](matrix/max_area_of_island.py)
@@ -863,6 +863,7 @@
863863
* [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py)
864864
* [Photoelectric Effect](physics/photoelectric_effect.py)
865865
* [Potential Energy](physics/potential_energy.py)
866+
* [Rainfall Intensity](physics/rainfall_intensity.py)
866867
* [Reynolds Number](physics/reynolds_number.py)
867868
* [Rms Speed Of Molecule](physics/rms_speed_of_molecule.py)
868869
* [Shear Stress](physics/shear_stress.py)
@@ -1259,6 +1260,7 @@
12591260
* [Can String Be Rearranged As Palindrome](strings/can_string_be_rearranged_as_palindrome.py)
12601261
* [Capitalize](strings/capitalize.py)
12611262
* [Check Anagrams](strings/check_anagrams.py)
1263+
* [Count Vowels](strings/count_vowels.py)
12621264
* [Credit Card Validator](strings/credit_card_validator.py)
12631265
* [Damerau Levenshtein Distance](strings/damerau_levenshtein_distance.py)
12641266
* [Detecting English Programmatically](strings/detecting_english_programmatically.py)

audio_filters/show_response.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from abc import abstractmethod
34
from math import pi
45
from typing import Protocol
56

@@ -8,14 +9,14 @@
89

910

1011
class FilterType(Protocol):
12+
@abstractmethod
1113
def process(self, sample: float) -> float:
1214
"""
1315
Calculate y[n]
1416
1517
>>> issubclass(FilterType, Protocol)
1618
True
1719
"""
18-
return 0.0
1920

2021

2122
def get_bounds(

backtracking/all_permutations.py

+36
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,42 @@ def create_state_space_tree(
2323
Creates a state space tree to iterate through each branch using DFS.
2424
We know that each state has exactly len(sequence) - index children.
2525
It terminates when it reaches the end of the given sequence.
26+
27+
:param sequence: The input sequence for which permutations are generated.
28+
:param current_sequence: The current permutation being built.
29+
:param index: The current index in the sequence.
30+
:param index_used: list to track which elements are used in permutation.
31+
32+
Example 1:
33+
>>> sequence = [1, 2, 3]
34+
>>> current_sequence = []
35+
>>> index_used = [False, False, False]
36+
>>> create_state_space_tree(sequence, current_sequence, 0, index_used)
37+
[1, 2, 3]
38+
[1, 3, 2]
39+
[2, 1, 3]
40+
[2, 3, 1]
41+
[3, 1, 2]
42+
[3, 2, 1]
43+
44+
Example 2:
45+
>>> sequence = ["A", "B", "C"]
46+
>>> current_sequence = []
47+
>>> index_used = [False, False, False]
48+
>>> create_state_space_tree(sequence, current_sequence, 0, index_used)
49+
['A', 'B', 'C']
50+
['A', 'C', 'B']
51+
['B', 'A', 'C']
52+
['B', 'C', 'A']
53+
['C', 'A', 'B']
54+
['C', 'B', 'A']
55+
56+
Example 3:
57+
>>> sequence = [1]
58+
>>> current_sequence = []
59+
>>> index_used = [False]
60+
>>> create_state_space_tree(sequence, current_sequence, 0, index_used)
61+
[1]
2662
"""
2763

2864
if index == len(sequence):

backtracking/all_subsequences.py

+51-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,56 @@ def create_state_space_tree(
2222
Creates a state space tree to iterate through each branch using DFS.
2323
We know that each state has exactly two children.
2424
It terminates when it reaches the end of the given sequence.
25+
26+
:param sequence: The input sequence for which subsequences are generated.
27+
:param current_subsequence: The current subsequence being built.
28+
:param index: The current index in the sequence.
29+
30+
Example:
31+
>>> sequence = [3, 2, 1]
32+
>>> current_subsequence = []
33+
>>> create_state_space_tree(sequence, current_subsequence, 0)
34+
[]
35+
[1]
36+
[2]
37+
[2, 1]
38+
[3]
39+
[3, 1]
40+
[3, 2]
41+
[3, 2, 1]
42+
43+
>>> sequence = ["A", "B"]
44+
>>> current_subsequence = []
45+
>>> create_state_space_tree(sequence, current_subsequence, 0)
46+
[]
47+
['B']
48+
['A']
49+
['A', 'B']
50+
51+
>>> sequence = []
52+
>>> current_subsequence = []
53+
>>> create_state_space_tree(sequence, current_subsequence, 0)
54+
[]
55+
56+
>>> sequence = [1, 2, 3, 4]
57+
>>> current_subsequence = []
58+
>>> create_state_space_tree(sequence, current_subsequence, 0)
59+
[]
60+
[4]
61+
[3]
62+
[3, 4]
63+
[2]
64+
[2, 4]
65+
[2, 3]
66+
[2, 3, 4]
67+
[1]
68+
[1, 4]
69+
[1, 3]
70+
[1, 3, 4]
71+
[1, 2]
72+
[1, 2, 4]
73+
[1, 2, 3]
74+
[1, 2, 3, 4]
2575
"""
2676

2777
if index == len(sequence):
@@ -35,7 +85,7 @@ def create_state_space_tree(
3585

3686

3787
if __name__ == "__main__":
38-
seq: list[Any] = [3, 1, 2, 4]
88+
seq: list[Any] = [1, 2, 3]
3989
generate_all_subsequences(seq)
4090

4191
seq.clear()

backtracking/knight_tour.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ def get_valid_pos(position: tuple[int, int], n: int) -> list[tuple[int, int]]:
2424
]
2525
permissible_positions = []
2626

27-
for position in positions:
28-
y_test, x_test = position
27+
for inner_position in positions:
28+
y_test, x_test = inner_position
2929
if 0 <= y_test < n and 0 <= x_test < n:
30-
permissible_positions.append(position)
30+
permissible_positions.append(inner_position)
3131

3232
return permissible_positions
3333

backtracking/sudoku.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
Given a partially filled 9×9 2D array, the objective is to fill a 9×9
2+
Given a partially filled 9x9 2D array, the objective is to fill a 9x9
33
square grid with digits numbered 1 to 9, so that every row, column, and
4-
and each of the nine 3×3 sub-grids contains all of the digits.
4+
and each of the nine 3x3 sub-grids contains all of the digits.
55
66
This can be solved using Backtracking and is similar to n-queens.
77
We check to see if a cell is safe or not and recursively call the

bit_manipulation/binary_and_operator.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def binary_and(a: int, b: int) -> str:
2626
>>> binary_and(0, 1.1)
2727
Traceback (most recent call last):
2828
...
29-
TypeError: 'float' object cannot be interpreted as an integer
29+
ValueError: Unknown format code 'b' for object of type 'float'
3030
>>> binary_and("0", "1")
3131
Traceback (most recent call last):
3232
...
@@ -35,8 +35,8 @@ def binary_and(a: int, b: int) -> str:
3535
if a < 0 or b < 0:
3636
raise ValueError("the value of both inputs must be positive")
3737

38-
a_binary = str(bin(a))[2:] # remove the leading "0b"
39-
b_binary = str(bin(b))[2:] # remove the leading "0b"
38+
a_binary = format(a, "b")
39+
b_binary = format(b, "b")
4040

4141
max_len = max(len(a_binary), len(b_binary))
4242

bit_manipulation/single_bit_manipulation_operations.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def set_bit(number: int, position: int) -> int:
88
Set the bit at position to 1.
99
1010
Details: perform bitwise or for given number and X.
11-
Where X is a number with all the bits zeroes and bit on given
12-
position one.
11+
Where X is a number with all the bits - zeroes and bit on given
12+
position - one.
1313
1414
>>> set_bit(0b1101, 1) # 0b1111
1515
15
@@ -26,8 +26,8 @@ def clear_bit(number: int, position: int) -> int:
2626
Set the bit at position to 0.
2727
2828
Details: perform bitwise and for given number and X.
29-
Where X is a number with all the bits ones and bit on given
30-
position zero.
29+
Where X is a number with all the bits - ones and bit on given
30+
position - zero.
3131
3232
>>> clear_bit(0b10010, 1) # 0b10000
3333
16
@@ -42,8 +42,8 @@ def flip_bit(number: int, position: int) -> int:
4242
Flip the bit at position.
4343
4444
Details: perform bitwise xor for given number and X.
45-
Where X is a number with all the bits zeroes and bit on given
46-
position one.
45+
Where X is a number with all the bits - zeroes and bit on given
46+
position - one.
4747
4848
>>> flip_bit(0b101, 1) # 0b111
4949
7
@@ -79,7 +79,7 @@ def get_bit(number: int, position: int) -> int:
7979
Get the bit at the given position
8080
8181
Details: perform bitwise and for the given number and X,
82-
Where X is a number with all the bits zeroes and bit on given position one.
82+
Where X is a number with all the bits - zeroes and bit on given position - one.
8383
If the result is not equal to 0, then the bit on the given position is 1, else 0.
8484
8585
>>> get_bit(0b1010, 0)

compression/burrows_wheeler.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform
33
4-
The BurrowsWheeler transform (BWT, also called block-sorting compression)
4+
The Burrows-Wheeler transform (BWT, also called block-sorting compression)
55
rearranges a character string into runs of similar characters. This is useful
66
for compression, since it tends to be easy to compress a string that has runs
77
of repeated characters by techniques such as move-to-front transform and

compression/huffman.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def build_tree(letters: list[Letter]) -> Letter | TreeNode:
4040
Run through the list of Letters and build the min heap
4141
for the Huffman Tree.
4242
"""
43-
response: list[Letter | TreeNode] = letters # type: ignore
43+
response: list[Letter | TreeNode] = list(letters)
4444
while len(response) > 1:
4545
left = response.pop(0)
4646
right = response.pop(0)
@@ -59,7 +59,7 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]:
5959
if isinstance(root, Letter):
6060
root.bitstring[root.letter] = bitstring
6161
return [root]
62-
treenode: TreeNode = root # type: ignore
62+
treenode: TreeNode = root
6363
letters = []
6464
letters += traverse_tree(treenode.left, bitstring + "0")
6565
letters += traverse_tree(treenode.right, bitstring + "1")

compression/lempel_ziv.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
One of the several implementations of LempelZivWelch compression algorithm
2+
One of the several implementations of Lempel-Ziv-Welch compression algorithm
33
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
44
"""
55

@@ -43,7 +43,7 @@ def add_key_to_lexicon(
4343

4444
def compress_data(data_bits: str) -> str:
4545
"""
46-
Compresses given data_bits using LempelZivWelch compression algorithm
46+
Compresses given data_bits using Lempel-Ziv-Welch compression algorithm
4747
and returns the result as a string
4848
"""
4949
lexicon = {"0": "0", "1": "1"}

compression/lempel_ziv_decompress.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
One of the several implementations of LempelZivWelch decompression algorithm
2+
One of the several implementations of Lempel-Ziv-Welch decompression algorithm
33
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
44
"""
55

@@ -26,7 +26,7 @@ def read_file_binary(file_path: str) -> str:
2626

2727
def decompress_data(data_bits: str) -> str:
2828
"""
29-
Decompresses given data_bits using LempelZivWelch compression algorithm
29+
Decompresses given data_bits using Lempel-Ziv-Welch compression algorithm
3030
and returns the result as a string
3131
"""
3232
lexicon = {"0": "0", "1": "1"}

computer_vision/cnn_classification.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
# Importing the Keras libraries and packages
2727
import tensorflow as tf
28-
from tensorflow.keras import layers, models
28+
from keras import layers, models
2929

3030
if __name__ == "__main__":
3131
# Initialising the CNN

computer_vision/haralick_descriptors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def transform(
141141

142142
center_x, center_y = (x // 2 for x in kernel.shape)
143143

144-
# Use padded image when applying convolotion
144+
# Use padded image when applying convolution
145145
# to not go out of bounds of the original the image
146146
transformed = np.zeros(image.shape, dtype=np.uint8)
147147
padded = np.pad(image, 1, "constant", constant_values=constant)

conversions/weight_conversion.py

+6
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ def weight_conversion(from_type: str, to_type: str, value: float) -> float:
297297
1.660540199e-23
298298
>>> weight_conversion("atomic-mass-unit","atomic-mass-unit",2)
299299
1.999999998903455
300+
>>> weight_conversion("slug", "kilogram", 1)
301+
Traceback (most recent call last):
302+
...
303+
ValueError: Invalid 'from_type' or 'to_type' value: 'slug', 'kilogram'
304+
Supported values are: kilogram, gram, milligram, metric-ton, long-ton, short-ton, \
305+
pound, stone, ounce, carrat, atomic-mass-unit
300306
"""
301307
if to_type not in KILOGRAM_CHART or from_type not in WEIGHT_TYPE_CHART:
302308
msg = (

data_structures/arrays/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)