Skip to content

Commit 299acbc

Browse files
committed
Fix ruff lint issues
1 parent 0776097 commit 299acbc

File tree

13 files changed

+22
-24
lines changed

13 files changed

+22
-24
lines changed

digital_image_processing/filters/local_binary_pattern.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def get_neighbors_pixel(
1919

2020
try:
2121
return int(image[x_coordinate][y_coordinate] >= center)
22-
except (IndexError, TypeError):
22+
except IndexError, TypeError:
2323
return 0
2424

2525

divide_and_conquer/convex_hull.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def _construct_points(
124124
else:
125125
try:
126126
points.append(Point(p[0], p[1]))
127-
except (IndexError, TypeError):
127+
except IndexError, TypeError:
128128
print(
129129
f"Ignoring deformed point {p}. All points"
130130
" must have at least 2 coordinates."

dynamic_programming/catalan_numbers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def catalan_numbers(upper_limit: int) -> "list[int]":
7171
print(f"The Catalan numbers from 0 through {N} are:")
7272
print(catalan_numbers(N))
7373
print("Try another upper limit for the sequence: ", end="")
74-
except (NameError, ValueError):
74+
except NameError, ValueError:
7575
print("\n********* Invalid input, goodbye! ************\n")
7676

7777
import doctest

machine_learning/decision_tree.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
Output: The decision tree maps a real number input to a real number output.
55
"""
66

7-
import numpy as np
87
from collections import Counter
98

9+
import numpy as np
10+
1011

1112
class DecisionTree:
1213
def __init__(self, depth=5, min_leaf_size=5, task="regression", criterion="gini"):
@@ -54,7 +55,7 @@ def gini(self, y):
5455
5556
Lower Gini value indicates better purity (best split).
5657
"""
57-
classes, counts = np.unique(y, return_counts=True)
58+
_, counts = np.unique(y, return_counts=True)
5859
prob = counts / counts.sum()
5960
return 1 - np.sum(prob**2)
6061

@@ -67,7 +68,7 @@ def entropy(self, y):
6768
6869
Lower entropy means higher purity.
6970
"""
70-
classes, counts = np.unique(y, return_counts=True)
71+
_, counts = np.unique(y, return_counts=True)
7172
prob = counts / counts.sum()
7273
return -np.sum(prob * np.log2(prob + 1e-9))
7374

@@ -76,7 +77,8 @@ def information_gain(self, parent, left, right):
7677
Computes the information gain from splitting a dataset.
7778
Information gain represents the reduction in impurity
7879
after a dataset is split into left and right subsets.
79-
Formula: IG = Impurity(parent) - [weighted impurity(left) + weighted impurity(right)]
80+
Formula: IG = Impurity(parent) - [
81+
weighted impurity(left) + weighted impurity(right)]
8082
8183
Higher information gain indicates a better split.
8284
"""
@@ -155,10 +157,7 @@ def train(self, x, y):
155157
then the data set is not split and the average for the entire array is used as
156158
the predictor
157159
"""
158-
if self.task == "regression":
159-
best_score = float("inf")
160-
else:
161-
best_score = -float("inf")
160+
best_score = float("inf") if self.task == "regression" else -float("inf")
162161

163162
for i in range(len(x)):
164163
if len(x[:i]) < self.min_leaf_size:
@@ -209,11 +208,10 @@ def train(self, x, y):
209208
self.left.train(left_x, left_y)
210209
self.right.train(right_x, right_y)
211210

211+
elif self.task == "regression":
212+
self.prediction = np.mean(y)
212213
else:
213-
if self.task == "regression":
214-
self.prediction = np.mean(y)
215-
else:
216-
self.prediction = self.most_common_label(y)
214+
self.prediction = self.most_common_label(y)
217215

218216
def predict(self, x):
219217
"""

maths/greatest_common_divisor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def main():
7373
f"{greatest_common_divisor(num_1, num_2)}"
7474
)
7575
print(f"By iterative gcd({num_1}, {num_2}) = {gcd_by_iterative(num_1, num_2)}")
76-
except (IndexError, UnboundLocalError, ValueError):
76+
except IndexError, UnboundLocalError, ValueError:
7777
print("Wrong input")
7878

7979

project_euler/problem_002/sol4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def solution(n: int = 4000000) -> int:
5656

5757
try:
5858
n = int(n)
59-
except (TypeError, ValueError):
59+
except TypeError, ValueError:
6060
raise TypeError("Parameter n must be int or castable to int.")
6161
if n <= 0:
6262
raise ValueError("Parameter n must be greater than or equal to one.")

project_euler/problem_003/sol1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def solution(n: int = 600851475143) -> int:
8080

8181
try:
8282
n = int(n)
83-
except (TypeError, ValueError):
83+
except TypeError, ValueError:
8484
raise TypeError("Parameter n must be int or castable to int.")
8585
if n <= 0:
8686
raise ValueError("Parameter n must be greater than or equal to one.")

project_euler/problem_003/sol2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def solution(n: int = 600851475143) -> int:
4444

4545
try:
4646
n = int(n)
47-
except (TypeError, ValueError):
47+
except TypeError, ValueError:
4848
raise TypeError("Parameter n must be int or castable to int.")
4949
if n <= 0:
5050
raise ValueError("Parameter n must be greater than or equal to one.")

project_euler/problem_003/sol3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def solution(n: int = 600851475143) -> int:
4444

4545
try:
4646
n = int(n)
47-
except (TypeError, ValueError):
47+
except TypeError, ValueError:
4848
raise TypeError("Parameter n must be int or castable to int.")
4949
if n <= 0:
5050
raise ValueError("Parameter n must be greater than or equal to one.")

project_euler/problem_005/sol1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def solution(n: int = 20) -> int:
4747

4848
try:
4949
n = int(n)
50-
except (TypeError, ValueError):
50+
except TypeError, ValueError:
5151
raise TypeError("Parameter n must be int or castable to int.")
5252
if n <= 0:
5353
raise ValueError("Parameter n must be greater than or equal to one.")

0 commit comments

Comments
 (0)