Skip to content

Commit 0177ae1

Browse files
authored
Upgrade to Python 3.13 (TheAlgorithms#11588)
1 parent a7bfa22 commit 0177ae1

35 files changed

+135
-131
lines changed

.github/workflows/build.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- uses: actions/checkout@v4
1313
- uses: actions/setup-python@v5
1414
with:
15-
python-version: 3.12
15+
python-version: 3.13
1616
allow-prereleases: true
1717
- uses: actions/cache@v4
1818
with:
@@ -26,6 +26,10 @@ jobs:
2626
# TODO: #8818 Re-enable quantum tests
2727
run: pytest
2828
--ignore=quantum/q_fourier_transform.py
29+
--ignore=computer_vision/cnn_classification.py
30+
--ignore=dynamic_programming/k_means_clustering_tensorflow.py
31+
--ignore=machine_learning/lstm/lstm_prediction.py
32+
--ignore=neural_network/input_data.py
2933
--ignore=project_euler/
3034
--ignore=scripts/validate_solutions.py
3135
--cov-report=term-missing:skip-covered

DIRECTORY.md

-1
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,6 @@
13431343
* [Get Ip Geolocation](web_programming/get_ip_geolocation.py)
13441344
* [Get Top Billionaires](web_programming/get_top_billionaires.py)
13451345
* [Get Top Hn Posts](web_programming/get_top_hn_posts.py)
1346-
* [Get User Tweets](web_programming/get_user_tweets.py)
13471346
* [Giphy](web_programming/giphy.py)
13481347
* [Instagram Crawler](web_programming/instagram_crawler.py)
13491348
* [Instagram Pic](web_programming/instagram_pic.py)

computer_vision/haralick_descriptors.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def root_mean_square_error(original: np.ndarray, reference: np.ndarray) -> float
1919
>>> root_mean_square_error(np.array([1, 2, 3]), np.array([6, 4, 2]))
2020
3.1622776601683795
2121
"""
22-
return np.sqrt(((original - reference) ** 2).mean())
22+
return float(np.sqrt(((original - reference) ** 2).mean()))
2323

2424

2525
def normalize_image(
@@ -273,7 +273,7 @@ def haralick_descriptors(matrix: np.ndarray) -> list[float]:
273273
>>> morphological = opening_filter(binary)
274274
>>> mask_1 = binary_mask(gray, morphological)[0]
275275
>>> concurrency = matrix_concurrency(mask_1, (0, 1))
276-
>>> haralick_descriptors(concurrency)
276+
>>> [float(f) for f in haralick_descriptors(concurrency)]
277277
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
278278
"""
279279
# Function np.indices could be used for bigger input types,
@@ -335,7 +335,7 @@ def get_descriptors(
335335
return np.concatenate(descriptors, axis=None)
336336

337337

338-
def euclidean(point_1: np.ndarray, point_2: np.ndarray) -> np.float32:
338+
def euclidean(point_1: np.ndarray, point_2: np.ndarray) -> float:
339339
"""
340340
Simple method for calculating the euclidean distance between two points,
341341
with type np.ndarray.
@@ -346,7 +346,7 @@ def euclidean(point_1: np.ndarray, point_2: np.ndarray) -> np.float32:
346346
>>> euclidean(a, b)
347347
3.3166247903554
348348
"""
349-
return np.sqrt(np.sum(np.square(point_1 - point_2)))
349+
return float(np.sqrt(np.sum(np.square(point_1 - point_2))))
350350

351351

352352
def get_distances(descriptors: np.ndarray, base: int) -> list[tuple[int, float]]:

data_structures/heap/binomial_heap.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class BinomialHeap:
7373
30
7474
7575
Deleting - delete() test
76-
>>> [first_heap.delete_min() for _ in range(20)]
76+
>>> [int(first_heap.delete_min()) for _ in range(20)]
7777
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
7878
7979
Create a new Heap
@@ -118,7 +118,7 @@ class BinomialHeap:
118118
values in merged heap; (merge is inplace)
119119
>>> results = []
120120
>>> while not first_heap.is_empty():
121-
... results.append(first_heap.delete_min())
121+
... results.append(int(first_heap.delete_min()))
122122
>>> results
123123
[17, 20, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 31, 34]
124124
"""
@@ -354,7 +354,7 @@ def delete_min(self):
354354
# Merge heaps
355355
self.merge_heaps(new_heap)
356356

357-
return min_value
357+
return int(min_value)
358358

359359
def pre_order(self):
360360
"""

electronics/circular_convolution.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def circular_convolution(self) -> list[float]:
3939
Usage:
4040
>>> convolution = CircularConvolution()
4141
>>> convolution.circular_convolution()
42-
[10, 10, 6, 14]
42+
[10.0, 10.0, 6.0, 14.0]
4343
4444
>>> convolution.first_signal = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6]
4545
>>> convolution.second_signal = [0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5]
@@ -54,7 +54,7 @@ def circular_convolution(self) -> list[float]:
5454
>>> convolution.first_signal = [1, -1, 2, 3, -1]
5555
>>> convolution.second_signal = [1, 2, 3]
5656
>>> convolution.circular_convolution()
57-
[8, -2, 3, 4, 11]
57+
[8.0, -2.0, 3.0, 4.0, 11.0]
5858
5959
"""
6060

@@ -91,7 +91,7 @@ def circular_convolution(self) -> list[float]:
9191
final_signal = np.matmul(np.transpose(matrix), np.transpose(self.first_signal))
9292

9393
# rounding-off to two decimal places
94-
return [round(i, 2) for i in final_signal]
94+
return [float(round(i, 2)) for i in final_signal]
9595

9696

9797
if __name__ == "__main__":

fractals/julia_sets.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
def eval_exponential(c_parameter: complex, z_values: np.ndarray) -> np.ndarray:
4141
"""
4242
Evaluate $e^z + c$.
43-
>>> eval_exponential(0, 0)
43+
>>> float(eval_exponential(0, 0))
4444
1.0
45-
>>> abs(eval_exponential(1, np.pi*1.j)) < 1e-15
45+
>>> bool(abs(eval_exponential(1, np.pi*1.j)) < 1e-15)
4646
True
47-
>>> abs(eval_exponential(1.j, 0)-1-1.j) < 1e-15
47+
>>> bool(abs(eval_exponential(1.j, 0)-1-1.j) < 1e-15)
4848
True
4949
"""
5050
return np.exp(z_values) + c_parameter
@@ -98,20 +98,20 @@ def iterate_function(
9898
9999
>>> iterate_function(eval_quadratic_polynomial, 0, 3, np.array([0,1,2])).shape
100100
(3,)
101-
>>> np.round(iterate_function(eval_quadratic_polynomial,
101+
>>> complex(np.round(iterate_function(eval_quadratic_polynomial,
102102
... 0,
103103
... 3,
104-
... np.array([0,1,2]))[0])
104+
... np.array([0,1,2]))[0]))
105105
0j
106-
>>> np.round(iterate_function(eval_quadratic_polynomial,
106+
>>> complex(np.round(iterate_function(eval_quadratic_polynomial,
107107
... 0,
108108
... 3,
109-
... np.array([0,1,2]))[1])
109+
... np.array([0,1,2]))[1]))
110110
(1+0j)
111-
>>> np.round(iterate_function(eval_quadratic_polynomial,
111+
>>> complex(np.round(iterate_function(eval_quadratic_polynomial,
112112
... 0,
113113
... 3,
114-
... np.array([0,1,2]))[2])
114+
... np.array([0,1,2]))[2]))
115115
(256+0j)
116116
"""
117117

graphics/bezier_curve.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def basis_function(self, t: float) -> list[float]:
3030
returns the x, y values of basis function at time t
3131
3232
>>> curve = BezierCurve([(1,1), (1,2)])
33-
>>> curve.basis_function(0)
33+
>>> [float(x) for x in curve.basis_function(0)]
3434
[1.0, 0.0]
35-
>>> curve.basis_function(1)
35+
>>> [float(x) for x in curve.basis_function(1)]
3636
[0.0, 1.0]
3737
"""
3838
assert 0 <= t <= 1, "Time t must be between 0 and 1."
@@ -55,9 +55,9 @@ def bezier_curve_function(self, t: float) -> tuple[float, float]:
5555
The last point in the curve is when t = 1.
5656
5757
>>> curve = BezierCurve([(1,1), (1,2)])
58-
>>> curve.bezier_curve_function(0)
58+
>>> tuple(float(x) for x in curve.bezier_curve_function(0))
5959
(1.0, 1.0)
60-
>>> curve.bezier_curve_function(1)
60+
>>> tuple(float(x) for x in curve.bezier_curve_function(1))
6161
(1.0, 2.0)
6262
"""
6363

graphs/dijkstra_binary_grid.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def dijkstra(
6969
x, y = predecessors[x, y]
7070
path.append(source) # add the source manually
7171
path.reverse()
72-
return matrix[destination], path
72+
return float(matrix[destination]), path
7373

7474
for i in range(len(dx)):
7575
nx, ny = x + dx[i], y + dy[i]

linear_algebra/src/power_iteration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def power_iteration(
7878
if is_complex:
7979
lambda_ = np.real(lambda_)
8080

81-
return lambda_, vector
81+
return float(lambda_), vector
8282

8383

8484
def test_power_iteration() -> None:

linear_programming/simplex.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def generate_col_titles(self) -> list[str]:
107107

108108
def find_pivot(self) -> tuple[Any, Any]:
109109
"""Finds the pivot row and column.
110-
>>> Tableau(np.array([[-2,1,0,0,0], [3,1,1,0,6], [1,2,0,1,7.]]),
111-
... 2, 0).find_pivot()
110+
>>> tuple(int(x) for x in Tableau(np.array([[-2,1,0,0,0], [3,1,1,0,6],
111+
... [1,2,0,1,7.]]), 2, 0).find_pivot())
112112
(1, 0)
113113
"""
114114
objective = self.objectives[-1]
@@ -215,56 +215,56 @@ def run_simplex(self) -> dict[Any, Any]:
215215
Max: x1 + x2
216216
ST: x1 + 3x2 <= 4
217217
3x1 + x2 <= 4
218-
>>> Tableau(np.array([[-1,-1,0,0,0],[1,3,1,0,4],[3,1,0,1,4.]]),
219-
... 2, 0).run_simplex()
218+
>>> {key: float(value) for key, value in Tableau(np.array([[-1,-1,0,0,0],
219+
... [1,3,1,0,4],[3,1,0,1,4.]]), 2, 0).run_simplex().items()}
220220
{'P': 2.0, 'x1': 1.0, 'x2': 1.0}
221221
222222
# Standard linear program with 3 variables:
223223
Max: 3x1 + x2 + 3x3
224224
ST: 2x1 + x2 + x3 ≤ 2
225225
x1 + 2x2 + 3x3 ≤ 5
226226
2x1 + 2x2 + x3 ≤ 6
227-
>>> Tableau(np.array([
227+
>>> {key: float(value) for key, value in Tableau(np.array([
228228
... [-3,-1,-3,0,0,0,0],
229229
... [2,1,1,1,0,0,2],
230230
... [1,2,3,0,1,0,5],
231231
... [2,2,1,0,0,1,6.]
232-
... ]),3,0).run_simplex() # doctest: +ELLIPSIS
232+
... ]),3,0).run_simplex().items()} # doctest: +ELLIPSIS
233233
{'P': 5.4, 'x1': 0.199..., 'x3': 1.6}
234234
235235
236236
# Optimal tableau input:
237-
>>> Tableau(np.array([
237+
>>> {key: float(value) for key, value in Tableau(np.array([
238238
... [0, 0, 0.25, 0.25, 2],
239239
... [0, 1, 0.375, -0.125, 1],
240240
... [1, 0, -0.125, 0.375, 1]
241-
... ]), 2, 0).run_simplex()
241+
... ]), 2, 0).run_simplex().items()}
242242
{'P': 2.0, 'x1': 1.0, 'x2': 1.0}
243243
244244
# Non-standard: >= constraints
245245
Max: 2x1 + 3x2 + x3
246246
ST: x1 + x2 + x3 <= 40
247247
2x1 + x2 - x3 >= 10
248248
- x2 + x3 >= 10
249-
>>> Tableau(np.array([
249+
>>> {key: float(value) for key, value in Tableau(np.array([
250250
... [2, 0, 0, 0, -1, -1, 0, 0, 20],
251251
... [-2, -3, -1, 0, 0, 0, 0, 0, 0],
252252
... [1, 1, 1, 1, 0, 0, 0, 0, 40],
253253
... [2, 1, -1, 0, -1, 0, 1, 0, 10],
254254
... [0, -1, 1, 0, 0, -1, 0, 1, 10.]
255-
... ]), 3, 2).run_simplex()
255+
... ]), 3, 2).run_simplex().items()}
256256
{'P': 70.0, 'x1': 10.0, 'x2': 10.0, 'x3': 20.0}
257257
258258
# Non standard: minimisation and equalities
259259
Min: x1 + x2
260260
ST: 2x1 + x2 = 12
261261
6x1 + 5x2 = 40
262-
>>> Tableau(np.array([
262+
>>> {key: float(value) for key, value in Tableau(np.array([
263263
... [8, 6, 0, 0, 52],
264264
... [1, 1, 0, 0, 0],
265265
... [2, 1, 1, 0, 12],
266266
... [6, 5, 0, 1, 40.],
267-
... ]), 2, 2).run_simplex()
267+
... ]), 2, 2).run_simplex().items()}
268268
{'P': 7.0, 'x1': 5.0, 'x2': 2.0}
269269
270270
@@ -275,15 +275,15 @@ def run_simplex(self) -> dict[Any, Any]:
275275
2x1 + 4x2 <= 48
276276
x1 + x2 >= 10
277277
x1 >= 2
278-
>>> Tableau(np.array([
278+
>>> {key: float(value) for key, value in Tableau(np.array([
279279
... [2, 1, 0, 0, 0, -1, -1, 0, 0, 12.0],
280280
... [-8, -6, 0, 0, 0, 0, 0, 0, 0, 0.0],
281281
... [1, 3, 1, 0, 0, 0, 0, 0, 0, 33.0],
282282
... [4, 2, 0, 1, 0, 0, 0, 0, 0, 60.0],
283283
... [2, 4, 0, 0, 1, 0, 0, 0, 0, 48.0],
284284
... [1, 1, 0, 0, 0, -1, 0, 1, 0, 10.0],
285285
... [1, 0, 0, 0, 0, 0, -1, 0, 1, 2.0]
286-
... ]), 2, 2).run_simplex() # doctest: +ELLIPSIS
286+
... ]), 2, 2).run_simplex().items()} # doctest: +ELLIPSIS
287287
{'P': 132.0, 'x1': 12.000... 'x2': 5.999...}
288288
"""
289289
# Stop simplex algorithm from cycling.
@@ -307,11 +307,11 @@ def run_simplex(self) -> dict[Any, Any]:
307307
def interpret_tableau(self) -> dict[str, float]:
308308
"""Given the final tableau, add the corresponding values of the basic
309309
decision variables to the `output_dict`
310-
>>> Tableau(np.array([
310+
>>> {key: float(value) for key, value in Tableau(np.array([
311311
... [0,0,0.875,0.375,5],
312312
... [0,1,0.375,-0.125,1],
313313
... [1,0,-0.125,0.375,1]
314-
... ]),2, 0).interpret_tableau()
314+
... ]),2, 0).interpret_tableau().items()}
315315
{'P': 5.0, 'x1': 1.0, 'x2': 1.0}
316316
"""
317317
# P = RHS of final tableau

machine_learning/decision_tree.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ def mean_squared_error(self, labels, prediction):
2626
>>> tester = DecisionTree()
2727
>>> test_labels = np.array([1,2,3,4,5,6,7,8,9,10])
2828
>>> test_prediction = float(6)
29-
>>> tester.mean_squared_error(test_labels, test_prediction) == (
29+
>>> bool(tester.mean_squared_error(test_labels, test_prediction) == (
3030
... TestDecisionTree.helper_mean_squared_error_test(test_labels,
31-
... test_prediction))
31+
... test_prediction)))
3232
True
3333
>>> test_labels = np.array([1,2,3])
3434
>>> test_prediction = float(2)
35-
>>> tester.mean_squared_error(test_labels, test_prediction) == (
35+
>>> bool(tester.mean_squared_error(test_labels, test_prediction) == (
3636
... TestDecisionTree.helper_mean_squared_error_test(test_labels,
37-
... test_prediction))
37+
... test_prediction)))
3838
True
3939
"""
4040
if labels.ndim != 1:

machine_learning/forecasting/run.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def linear_regression_prediction(
2828
input : training data (date, total_user, total_event) in list of float
2929
output : list of total user prediction in float
3030
>>> n = linear_regression_prediction([2,3,4,5], [5,3,4,6], [3,1,2,4], [2,1], [2,2])
31-
>>> abs(n - 5.0) < 1e-6 # Checking precision because of floating point errors
31+
>>> bool(abs(n - 5.0) < 1e-6) # Checking precision because of floating point errors
3232
True
3333
"""
3434
x = np.array([[1, item, train_mtch[i]] for i, item in enumerate(train_dt)])
@@ -56,7 +56,7 @@ def sarimax_predictor(train_user: list, train_match: list, test_match: list) ->
5656
)
5757
model_fit = model.fit(disp=False, maxiter=600, method="nm")
5858
result = model_fit.predict(1, len(test_match), exog=[test_match])
59-
return result[0]
59+
return float(result[0])
6060

6161

6262
def support_vector_regressor(x_train: list, x_test: list, train_user: list) -> float:
@@ -75,7 +75,7 @@ def support_vector_regressor(x_train: list, x_test: list, train_user: list) -> f
7575
regressor = SVR(kernel="rbf", C=1, gamma=0.1, epsilon=0.1)
7676
regressor.fit(x_train, train_user)
7777
y_pred = regressor.predict(x_test)
78-
return y_pred[0]
78+
return float(y_pred[0])
7979

8080

8181
def interquartile_range_checker(train_user: list) -> float:
@@ -92,7 +92,7 @@ def interquartile_range_checker(train_user: list) -> float:
9292
q3 = np.percentile(train_user, 75)
9393
iqr = q3 - q1
9494
low_lim = q1 - (iqr * 0.1)
95-
return low_lim
95+
return float(low_lim)
9696

9797

9898
def data_safety_checker(list_vote: list, actual_result: float) -> bool:

machine_learning/k_nearest_neighbours.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _euclidean_distance(a: np.ndarray[float], b: np.ndarray[float]) -> float:
4242
>>> KNN._euclidean_distance(np.array([1, 2, 3]), np.array([1, 8, 11]))
4343
10.0
4444
"""
45-
return np.linalg.norm(a - b)
45+
return float(np.linalg.norm(a - b))
4646

4747
def classify(self, pred_point: np.ndarray[float], k: int = 5) -> str:
4848
"""

machine_learning/logistic_regression.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def sigmoid_function(z: float | np.ndarray) -> float | np.ndarray:
4545
@returns: returns value in the range 0 to 1
4646
4747
Examples:
48-
>>> sigmoid_function(4)
48+
>>> float(sigmoid_function(4))
4949
0.9820137900379085
5050
>>> sigmoid_function(np.array([-3, 3]))
5151
array([0.04742587, 0.95257413])
@@ -100,7 +100,7 @@ def cost_function(h: np.ndarray, y: np.ndarray) -> float:
100100
References:
101101
- https://en.wikipedia.org/wiki/Logistic_regression
102102
"""
103-
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
103+
return float((-y * np.log(h) - (1 - y) * np.log(1 - h)).mean())
104104

105105

106106
def log_likelihood(x, y, weights):

0 commit comments

Comments
 (0)