Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ae0fc85

Browse files
tianyizheng02github-actions
and
github-actions
authored
Fix ruff errors (TheAlgorithms#8936)
* Fix ruff errors Renamed neural_network/input_data.py to neural_network/input_data.py_tf because it should be left out of the directory for the following reasons: 1. Its sole purpose is to be used by neural_network/gan.py_tf, which is itself left out of the directory because of issues with TensorFlow. 2. It was taken directly from TensorFlow's codebase and is actually already deprecated. If/when neural_network/gan.py_tf is eventually re-added back to the directory, its implementation should be changed to not use neural_network/input_data.py anyway. * updating DIRECTORY.md * Change input_data.py_tf file extension Change input_data.py_tf file extension because algorithms-keeper bot is being picky about it --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 842d03f commit ae0fc85

21 files changed

+121
-94
lines changed

‎DIRECTORY.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,6 @@
710710
* [Exponential Linear Unit](neural_network/activation_functions/exponential_linear_unit.py)
711711
* [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py)
712712
* [Convolution Neural Network](neural_network/convolution_neural_network.py)
713-
* [Input Data](neural_network/input_data.py)
714713
* [Perceptron](neural_network/perceptron.py)
715714
* [Simple Neural Network](neural_network/simple_neural_network.py)
716715

‎conversions/length_conversion.py‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
-> Wikipedia reference: https://en.wikipedia.org/wiki/Millimeter
2323
"""
2424

25-
from collections import namedtuple
25+
from typing import NamedTuple
26+
27+
28+
class FromTo(NamedTuple):
29+
from_factor: float
30+
to_factor: float
2631

27-
from_to = namedtuple("from_to", "from_ to")
2832

2933
TYPE_CONVERSION = {
3034
"millimeter": "mm",
@@ -40,14 +44,14 @@
4044
}
4145

4246
METRIC_CONVERSION = {
43-
"mm": from_to(0.001, 1000),
44-
"cm": from_to(0.01, 100),
45-
"m": from_to(1, 1),
46-
"km": from_to(1000, 0.001),
47-
"in": from_to(0.0254, 39.3701),
48-
"ft": from_to(0.3048, 3.28084),
49-
"yd": from_to(0.9144, 1.09361),
50-
"mi": from_to(1609.34, 0.000621371),
47+
"mm": FromTo(0.001, 1000),
48+
"cm": FromTo(0.01, 100),
49+
"m": FromTo(1, 1),
50+
"km": FromTo(1000, 0.001),
51+
"in": FromTo(0.0254, 39.3701),
52+
"ft": FromTo(0.3048, 3.28084),
53+
"yd": FromTo(0.9144, 1.09361),
54+
"mi": FromTo(1609.34, 0.000621371),
5155
}
5256

5357

@@ -115,7 +119,11 @@ def length_conversion(value: float, from_type: str, to_type: str) -> float:
115119
f"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}"
116120
)
117121
raise ValueError(msg)
118-
return value * METRIC_CONVERSION[new_from].from_ * METRIC_CONVERSION[new_to].to
122+
return (
123+
value
124+
* METRIC_CONVERSION[new_from].from_factor
125+
* METRIC_CONVERSION[new_to].to_factor
126+
)
119127

120128

121129
if __name__ == "__main__":

‎conversions/pressure_conversions.py‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,23 @@
1919
-> https://www.unitconverters.net/pressure-converter.html
2020
"""
2121

22-
from collections import namedtuple
22+
from typing import NamedTuple
23+
24+
25+
class FromTo(NamedTuple):
26+
from_factor: float
27+
to_factor: float
2328

24-
from_to = namedtuple("from_to", "from_ to")
2529

2630
PRESSURE_CONVERSION = {
27-
"atm": from_to(1, 1),
28-
"pascal": from_to(0.0000098, 101325),
29-
"bar": from_to(0.986923, 1.01325),
30-
"kilopascal": from_to(0.00986923, 101.325),
31-
"megapascal": from_to(9.86923, 0.101325),
32-
"psi": from_to(0.068046, 14.6959),
33-
"inHg": from_to(0.0334211, 29.9213),
34-
"torr": from_to(0.00131579, 760),
31+
"atm": FromTo(1, 1),
32+
"pascal": FromTo(0.0000098, 101325),
33+
"bar": FromTo(0.986923, 1.01325),
34+
"kilopascal": FromTo(0.00986923, 101.325),
35+
"megapascal": FromTo(9.86923, 0.101325),
36+
"psi": FromTo(0.068046, 14.6959),
37+
"inHg": FromTo(0.0334211, 29.9213),
38+
"torr": FromTo(0.00131579, 760),
3539
}
3640

3741

@@ -71,7 +75,9 @@ def pressure_conversion(value: float, from_type: str, to_type: str) -> float:
7175
+ ", ".join(PRESSURE_CONVERSION)
7276
)
7377
return (
74-
value * PRESSURE_CONVERSION[from_type].from_ * PRESSURE_CONVERSION[to_type].to
78+
value
79+
* PRESSURE_CONVERSION[from_type].from_factor
80+
* PRESSURE_CONVERSION[to_type].to_factor
7581
)
7682

7783

‎conversions/volume_conversions.py‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,47 @@
1818
-> Wikipedia reference: https://en.wikipedia.org/wiki/Cup_(unit)
1919
"""
2020

21-
from collections import namedtuple
21+
from typing import NamedTuple
22+
23+
24+
class FromTo(NamedTuple):
25+
from_factor: float
26+
to_factor: float
2227

23-
from_to = namedtuple("from_to", "from_ to")
2428

2529
METRIC_CONVERSION = {
26-
"cubicmeter": from_to(1, 1),
27-
"litre": from_to(0.001, 1000),
28-
"kilolitre": from_to(1, 1),
29-
"gallon": from_to(0.00454, 264.172),
30-
"cubicyard": from_to(0.76455, 1.30795),
31-
"cubicfoot": from_to(0.028, 35.3147),
32-
"cup": from_to(0.000236588, 4226.75),
30+
"cubic meter": FromTo(1, 1),
31+
"litre": FromTo(0.001, 1000),
32+
"kilolitre": FromTo(1, 1),
33+
"gallon": FromTo(0.00454, 264.172),
34+
"cubic yard": FromTo(0.76455, 1.30795),
35+
"cubic foot": FromTo(0.028, 35.3147),
36+
"cup": FromTo(0.000236588, 4226.75),
3337
}
3438

3539

3640
def volume_conversion(value: float, from_type: str, to_type: str) -> float:
3741
"""
3842
Conversion between volume units.
39-
>>> volume_conversion(4, "cubicmeter", "litre")
43+
>>> volume_conversion(4, "cubic meter", "litre")
4044
4000
4145
>>> volume_conversion(1, "litre", "gallon")
4246
0.264172
43-
>>> volume_conversion(1, "kilolitre", "cubicmeter")
47+
>>> volume_conversion(1, "kilolitre", "cubic meter")
4448
1
45-
>>> volume_conversion(3, "gallon", "cubicyard")
49+
>>> volume_conversion(3, "gallon", "cubic yard")
4650
0.017814279
47-
>>> volume_conversion(2, "cubicyard", "litre")
51+
>>> volume_conversion(2, "cubic yard", "litre")
4852
1529.1
49-
>>> volume_conversion(4, "cubicfoot", "cup")
53+
>>> volume_conversion(4, "cubic foot", "cup")
5054
473.396
5155
>>> volume_conversion(1, "cup", "kilolitre")
5256
0.000236588
5357
>>> volume_conversion(4, "wrongUnit", "litre")
5458
Traceback (most recent call last):
5559
...
5660
ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are:
57-
cubicmeter, litre, kilolitre, gallon, cubicyard, cubicfoot, cup
61+
cubic meter, litre, kilolitre, gallon, cubic yard, cubic foot, cup
5862
"""
5963
if from_type not in METRIC_CONVERSION:
6064
raise ValueError(
@@ -66,7 +70,11 @@ def volume_conversion(value: float, from_type: str, to_type: str) -> float:
6670
f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n"
6771
+ ", ".join(METRIC_CONVERSION)
6872
)
69-
return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to
73+
return (
74+
value
75+
* METRIC_CONVERSION[from_type].from_factor
76+
* METRIC_CONVERSION[to_type].to_factor
77+
)
7078

7179

7280
if __name__ == "__main__":

‎data_structures/binary_tree/distribute_coins.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939

4040
from __future__ import annotations
4141

42-
from collections import namedtuple
4342
from dataclasses import dataclass
43+
from typing import NamedTuple
4444

4545

4646
@dataclass
@@ -50,7 +50,9 @@ class TreeNode:
5050
right: TreeNode | None = None
5151

5252

53-
CoinsDistribResult = namedtuple("CoinsDistribResult", "moves excess")
53+
class CoinsDistribResult(NamedTuple):
54+
moves: int
55+
excess: int
5456

5557

5658
def distribute_coins(root: TreeNode | None) -> int:
@@ -79,7 +81,7 @@ def distribute_coins(root: TreeNode | None) -> int:
7981
# Validation
8082
def count_nodes(node: TreeNode | None) -> int:
8183
"""
82-
>>> count_nodes(None):
84+
>>> count_nodes(None)
8385
0
8486
"""
8587
if node is None:
@@ -89,7 +91,7 @@ def count_nodes(node: TreeNode | None) -> int:
8991

9092
def count_coins(node: TreeNode | None) -> int:
9193
"""
92-
>>> count_coins(None):
94+
>>> count_coins(None)
9395
0
9496
"""
9597
if node is None:

‎electronics/electric_power.py‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# https://en.m.wikipedia.org/wiki/Electric_power
22
from __future__ import annotations
33

4-
from collections import namedtuple
4+
from typing import NamedTuple
5+
6+
7+
class Result(NamedTuple):
8+
name: str
9+
value: float
510

611

712
def electric_power(voltage: float, current: float, power: float) -> tuple:
@@ -10,11 +15,11 @@ def electric_power(voltage: float, current: float, power: float) -> tuple:
1015
fundamental value of electrical system.
1116
examples are below:
1217
>>> electric_power(voltage=0, current=2, power=5)
13-
result(name='voltage', value=2.5)
18+
Result(name='voltage', value=2.5)
1419
>>> electric_power(voltage=2, current=2, power=0)
15-
result(name='power', value=4.0)
20+
Result(name='power', value=4.0)
1621
>>> electric_power(voltage=-2, current=3, power=0)
17-
result(name='power', value=6.0)
22+
Result(name='power', value=6.0)
1823
>>> electric_power(voltage=2, current=4, power=2)
1924
Traceback (most recent call last):
2025
...
@@ -28,21 +33,20 @@ def electric_power(voltage: float, current: float, power: float) -> tuple:
2833
...
2934
ValueError: Power cannot be negative in any electrical/electronics system
3035
>>> electric_power(voltage=2.2, current=2.2, power=0)
31-
result(name='power', value=4.84)
36+
Result(name='power', value=4.84)
3237
"""
33-
result = namedtuple("result", "name value")
3438
if (voltage, current, power).count(0) != 1:
3539
raise ValueError("Only one argument must be 0")
3640
elif power < 0:
3741
raise ValueError(
3842
"Power cannot be negative in any electrical/electronics system"
3943
)
4044
elif voltage == 0:
41-
return result("voltage", power / current)
45+
return Result("voltage", power / current)
4246
elif current == 0:
43-
return result("current", power / voltage)
47+
return Result("current", power / voltage)
4448
elif power == 0:
45-
return result("power", float(round(abs(voltage * current), 2)))
49+
return Result("power", float(round(abs(voltage * current), 2)))
4650
else:
4751
raise ValueError("Exactly one argument must be 0")
4852

‎graphs/bi_directional_dijkstra.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def pass_and_relaxation(
2626
cst_bwd: dict,
2727
queue: PriorityQueue,
2828
parent: dict,
29-
shortest_distance: float|int,
30-
) -> float|int:
29+
shortest_distance: float,
30+
) -> float:
3131
for nxt, d in graph[v]:
3232
if nxt in visited_forward:
3333
continue

‎maths/area_under_curve.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88

99
def trapezoidal_area(
10-
fnc: Callable[[int|float],int| float],
11-
x_start: int|float,
12-
x_end: int|float,
10+
fnc: Callable[[float], float],
11+
x_start: float,
12+
x_end: float,
1313
steps: int = 100,
1414
) -> float:
1515
"""

‎maths/decimal_to_fraction.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def decimal_to_fraction(decimal: int|float | str) -> tuple[int, int]:
1+
def decimal_to_fraction(decimal: float | str) -> tuple[int, int]:
22
"""
33
Return a decimal number in its simplest fraction form
44
>>> decimal_to_fraction(2)

‎maths/line_length.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66

77
def line_length(
8-
fnc: Callable[[int|float],int| float],
9-
x_start: int|float,
10-
x_end: int|float,
8+
fnc: Callable[[float], float],
9+
x_start: float,
10+
x_end: float,
1111
steps: int = 100,
1212
) -> float:
1313
"""

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /