Skip to content

Commit 4700297

Browse files
Enable ruff RUF002 rule (TheAlgorithms#11377)
* Enable ruff RUF002 rule * Fix --------- Co-authored-by: Christian Clauss <[email protected]>
1 parent 79dc7c9 commit 4700297

File tree

69 files changed

+132
-131
lines changed

Some content is hidden

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

69 files changed

+132
-131
lines changed

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/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/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"}

data_structures/binary_tree/red_black_tree.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class RedBlackTree:
1717
and slower for reading in the average case, though, because they're
1818
both balanced binary search trees, both will get the same asymptotic
1919
performance.
20-
To read more about them, https://en.wikipedia.org/wiki/Redblack_tree
20+
To read more about them, https://en.wikipedia.org/wiki/Red-black_tree
2121
Unless otherwise specified, all asymptotic runtimes are specified in
2222
terms of the size of the tree.
2323
"""

digital_image_processing/edge_detection/canny.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ def detect_high_low_threshold(
7474
image_shape, destination, threshold_low, threshold_high, weak, strong
7575
):
7676
"""
77-
High-Low threshold detection. If an edge pixels gradient value is higher
77+
High-Low threshold detection. If an edge pixel's gradient value is higher
7878
than the high threshold value, it is marked as a strong edge pixel. If an
79-
edge pixels gradient value is smaller than the high threshold value and
79+
edge pixel's gradient value is smaller than the high threshold value and
8080
larger than the low threshold value, it is marked as a weak edge pixel. If
8181
an edge pixel's value is smaller than the low threshold value, it will be
8282
suppressed.

digital_image_processing/index_calculation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def arv12(self):
182182
Atmospherically Resistant Vegetation Index 2
183183
https://www.indexdatabase.de/db/i-single.php?id=396
184184
:return: index
185-
0.18+1.17*(self.nirself.red)/(self.nir+self.red)
185+
-0.18+1.17*(self.nir-self.red)/(self.nir+self.red)
186186
"""
187187
return -0.18 + (1.17 * ((self.nir - self.red) / (self.nir + self.red)))
188188

dynamic_programming/combination_sum_iv.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
The basic idea is to go over recursively to find the way such that the sum
1919
of chosen elements is “tar”. For every element, we have two choices
2020
1. Include the element in our set of chosen elements.
21-
2. Dont include the element in our set of chosen elements.
21+
2. Don't include the element in our set of chosen elements.
2222
"""
2323

2424

electronics/coulombs_law.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def couloumbs_law(
2020
2121
Reference
2222
----------
23-
Coulomb (1785) "Premier mémoire sur lélectricité et le magnétisme,"
24-
Histoire de lAcadémie Royale des Sciences, pp. 569577.
23+
Coulomb (1785) "Premier mémoire sur l'électricité et le magnétisme,"
24+
Histoire de l'Académie Royale des Sciences, pp. 569-577.
2525
2626
Parameters
2727
----------

hashes/fletcher16.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
The Fletcher checksum is an algorithm for computing a position-dependent
3-
checksum devised by John G. Fletcher (19342012) at Lawrence Livermore Labs
3+
checksum devised by John G. Fletcher (1934-2012) at Lawrence Livermore Labs
44
in the late 1970s.[1] The objective of the Fletcher checksum was to
55
provide error-detection properties approaching those of a cyclic
66
redundancy check but with the lower computational effort associated

linear_algebra/lu_decomposition.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Lowerupper (LU) decomposition factors a matrix as a product of a lower
2+
Lower-upper (LU) decomposition factors a matrix as a product of a lower
33
triangular matrix and an upper triangular matrix. A square matrix has an LU
44
decomposition under the following conditions:
55
- If the matrix is invertible, then it has an LU decomposition if and only

linear_algebra/src/schur_complement.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def schur_complement(
1818
the pseudo_inv argument.
1919
2020
Link to Wiki: https://en.wikipedia.org/wiki/Schur_complement
21-
See also Convex Optimization Boyd and Vandenberghe, A.5.5
21+
See also Convex Optimization - Boyd and Vandenberghe, A.5.5
2222
>>> import numpy as np
2323
>>> a = np.array([[1, 2], [2, 1]])
2424
>>> b = np.array([[0, 3], [3, 0]])

machine_learning/polynomial_regression.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
β = (XᵀX)⁻¹Xᵀy = X⁺y
1313
14-
where X is the design matrix, y is the response vector, and X⁺ denotes the MoorePenrose
14+
where X is the design matrix, y is the response vector, and X⁺ denotes the Moore-Penrose
1515
pseudoinverse of X. In the case of polynomial regression, the design matrix is
1616
1717
|1 x₁ x₁² ⋯ x₁ᵐ|
@@ -106,7 +106,7 @@ def fit(self, x_train: np.ndarray, y_train: np.ndarray) -> None:
106106
107107
β = (XᵀX)⁻¹Xᵀy = X⁺y
108108
109-
where X⁺ denotes the MoorePenrose pseudoinverse of the design matrix X. This
109+
where X⁺ denotes the Moore-Penrose pseudoinverse of the design matrix X. This
110110
function computes X⁺ using singular value decomposition (SVD).
111111
112112
References:

maths/chudnovsky_algorithm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
def pi(precision: int) -> str:
66
"""
77
The Chudnovsky algorithm is a fast method for calculating the digits of PI,
8-
based on Ramanujans PI formulae.
8+
based on Ramanujan's PI formulae.
99
1010
https://en.wikipedia.org/wiki/Chudnovsky_algorithm
1111

maths/entropy.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ def calculate_prob(text: str) -> None:
2121
:return: Prints
2222
1) Entropy of information based on 1 alphabet
2323
2) Entropy of information based on couples of 2 alphabet
24-
3) print Entropy of H(X n∣Xn−1)
24+
3) print Entropy of H(X n|Xn-1)
2525
2626
Text from random books. Also, random quotes.
27-
>>> text = ("Behind Winstons back the voice "
27+
>>> text = ("Behind Winston's back the voice "
2828
... "from the telescreen was still "
2929
... "babbling and the overfulfilment")
3030
>>> calculate_prob(text)

maths/lucas_lehmer_primality_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""
2-
In mathematics, the LucasLehmer test (LLT) is a primality test for Mersenne
2+
In mathematics, the Lucas-Lehmer test (LLT) is a primality test for Mersenne
33
numbers. https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test
44
55
A Mersenne number is a number that is one less than a power of two.
66
That is M_p = 2^p - 1
77
https://en.wikipedia.org/wiki/Mersenne_prime
88
9-
The LucasLehmer test is the primality test used by the
9+
The Lucas-Lehmer test is the primality test used by the
1010
Great Internet Mersenne Prime Search (GIMPS) to locate large primes.
1111
"""
1212

maths/modular_division.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def modular_division(a: int, b: int, n: int) -> int:
99
GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
1010
1111
Given three integers a, b, and n, such that gcd(a,n)=1 and n>1, the algorithm should
12-
return an integer x such that 0≤x≤n1, and b/a=x(modn) (that is, b=ax(modn)).
12+
return an integer x such that 0≤x≤n-1, and b/a=x(modn) (that is, b=ax(modn)).
1313
1414
Theorem:
1515
a has a multiplicative inverse modulo n iff gcd(a,n) = 1

maths/numerical_analysis/bisection_2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Given a function on floating number f(x) and two floating numbers ‘a’ and ‘b’ such that
2+
Given a function on floating number f(x) and two floating numbers `a` and `b` such that
33
f(a) * f(b) < 0 and f(x) is continuous in [a, b].
44
Here f(x) represents algebraic or transcendental equation.
55
Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0)

maths/numerical_analysis/nevilles_method.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Python program to show how to interpolate and evaluate a polynomial
33
using Neville's method.
4-
Nevilles method evaluates a polynomial that passes through a
4+
Neville's method evaluates a polynomial that passes through a
55
given set of x and y points for a particular x value (x0) using the
66
Newton polynomial form.
77
Reference:

maths/simultaneous_linear_equation_solver.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
https://en.wikipedia.org/wiki/Augmented_matrix
33
44
This algorithm solves simultaneous linear equations of the form
5-
λa + λb + λc + λd + ... = γ as [λ, λ, λ, λ, ..., γ]
6-
Where λ & γ are individual coefficients, the no. of equations = no. of coefficients - 1
5+
λa + λb + λc + λd + ... = y as [λ, λ, λ, λ, ..., y]
6+
Where λ & y are individual coefficients, the no. of equations = no. of coefficients - 1
77
8-
Note in order to work there must exist 1 equation where all instances of λ and γ != 0
8+
Note in order to work there must exist 1 equation where all instances of λ and y != 0
99
"""
1010

1111

matrix/largest_square_area_in_matrix.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
3232
Approach:
3333
We initialize another matrix (dp) with the same dimensions
34-
as the original one initialized with all 0s.
34+
as the original one initialized with all 0's.
3535
3636
dp_array(i,j) represents the side length of the maximum square whose
3737
bottom right corner is the cell with index (i,j) in the original matrix.
3838
3939
Starting from index (0,0), for every 1 found in the original matrix,
4040
we update the value of the current element as
4141
42-
dp_array(i,j)=dp_array(dp(i1,j),dp_array(i1,j1),dp_array(i,j1)) + 1.
42+
dp_array(i,j)=dp_array(dp(i-1,j),dp_array(i-1,j-1),dp_array(i,j-1)) + 1.
4343
"""
4444

4545

matrix/spiral_print.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def spiral_traversal(matrix: list[list]) -> list[int]:
8989
Algorithm:
9090
Step 1. first pop the 0 index list. (which is [1,2,3,4] and concatenate the
9191
output of [step 2])
92-
Step 2. Now perform matrixs Transpose operation (Change rows to column
92+
Step 2. Now perform matrix's Transpose operation (Change rows to column
9393
and vice versa) and reverse the resultant matrix.
9494
Step 3. Pass the output of [2nd step], to same recursive function till
9595
base case hits.

neural_network/back_propagation_neural_network.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
"""
44
5-
A Framework of Back Propagation Neural Network(BP) model
5+
A Framework of Back Propagation Neural Network (BP) model
66
77
Easy to use:
8-
* add many layers as you want !!!
8+
* add many layers as you want ! ! !
99
* clearly see how the loss decreasing
1010
Easy to expand:
1111
* more activation functions

other/davis_putnam_logemann_loveland.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22

33
"""
4-
DavisPutnamLogemannLoveland (DPLL) algorithm is a complete, backtracking-based
4+
Davis-Putnam-Logemann-Loveland (DPLL) algorithm is a complete, backtracking-based
55
search algorithm for deciding the satisfiability of propositional logic formulae in
66
conjunctive normal form, i.e, for solving the Conjunctive Normal Form SATisfiability
77
(CNF-SAT) problem.

other/fischer_yates_shuffle.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/python
22
"""
3-
The FisherYates shuffle is an algorithm for generating a random permutation of a
3+
The Fisher-Yates shuffle is an algorithm for generating a random permutation of a
44
finite sequence.
55
For more details visit
66
wikipedia/Fischer-Yates-Shuffle.

physics/archimedes_principle_of_buoyant_force.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
fluid. This principle was discovered by the Greek mathematician Archimedes.
44
55
Equation for calculating buoyant force:
6-
Fb = ρ * V * g
6+
Fb = p * V * g
77
88
https://en.wikipedia.org/wiki/Archimedes%27_principle
99
"""

physics/center_of_mass.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
is the particle equivalent of a given object for the application of Newton's laws of
1717
motion.
1818
19-
In the case of a system of particles P_i, i = 1,..., n , each with mass m_i that are
20-
located in space with coordinates r_i, i = 1,..., n , the coordinates R of the center
19+
In the case of a system of particles P_i, i = 1, ..., n , each with mass m_i that are
20+
located in space with coordinates r_i, i = 1, ..., n , the coordinates R of the center
2121
of mass corresponds to:
2222
2323
R = (Σ(mi * ri) / Σ(mi))
@@ -36,8 +36,8 @@ def center_of_mass(particles: list[Particle]) -> Coord3D:
3636
Input Parameters
3737
----------------
3838
particles: list(Particle):
39-
A list of particles where each particle is a tuple with it´s (x, y, z) position and
40-
it´s mass.
39+
A list of particles where each particle is a tuple with it's (x, y, z) position and
40+
it's mass.
4141
4242
Returns
4343
-------

physics/centripetal_force.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
The unit of centripetal force is newton.
77
88
The centripetal force is always directed perpendicular to the
9-
direction of the objects displacement. Using Newtons second
9+
direction of the object's displacement. Using Newton's second
1010
law of motion, it is found that the centripetal force of an object
1111
moving in a circular path always acts towards the centre of the circle.
1212
The Centripetal Force Formula is given as the product of mass (in kg)

physics/lorentz_transformation_four_vector.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
with respect to X, then the Lorentz transformation from X to X' is X' = BX,
1313
where
1414
15-
| γ -γβ 0 0|
16-
B = |-γβ γ 0 0|
15+
| y -γβ 0 0|
16+
B = |-γβ y 0 0|
1717
| 0 0 1 0|
1818
| 0 0 0 1|
1919
2020
is the matrix describing the Lorentz boost between X and X',
21-
γ = 1 / √(1 - v²/c²) is the Lorentz factor, and β = v/c is the velocity as
21+
y = 1 / √(1 - v²/c²) is the Lorentz factor, and β = v/c is the velocity as
2222
a fraction of c.
2323
2424
Reference: https://en.wikipedia.org/wiki/Lorentz_transformation
@@ -63,7 +63,7 @@ def beta(velocity: float) -> float:
6363

6464
def gamma(velocity: float) -> float:
6565
"""
66-
Calculate the Lorentz factor γ = 1 / √(1 - v²/c²) for a given velocity
66+
Calculate the Lorentz factor y = 1 / √(1 - v²/c²) for a given velocity
6767
>>> gamma(4)
6868
1.0000000000000002
6969
>>> gamma(1e5)
@@ -90,12 +90,12 @@ def transformation_matrix(velocity: float) -> np.ndarray:
9090
"""
9191
Calculate the Lorentz transformation matrix for movement in the x direction:
9292
93-
| γ -γβ 0 0|
94-
|-γβ γ 0 0|
93+
| y -γβ 0 0|
94+
|-γβ y 0 0|
9595
| 0 0 1 0|
9696
| 0 0 0 1|
9797
98-
where γ is the Lorentz factor and β is the velocity as a fraction of c
98+
where y is the Lorentz factor and β is the velocity as a fraction of c
9999
>>> transformation_matrix(29979245)
100100
array([[ 1.00503781, -0.10050378, 0. , 0. ],
101101
[-0.10050378, 1.00503781, 0. , 0. ],

0 commit comments

Comments
 (0)