mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
f97161d001
commit
2a4ea96dbd
@ -15,6 +15,7 @@ def is_diagonally_dominant(matrix):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def gauss_seidel(matrix, vector, initial_guess, max_iterations=100, tol=1e-6):
|
||||
"""
|
||||
Solve a system of linear equations using the Gauss-Seidel method.
|
||||
@ -33,7 +34,9 @@ def gauss_seidel(matrix, vector, initial_guess, max_iterations=100, tol=1e-6):
|
||||
ValueError: If the matrix is not diagonally dominant.
|
||||
"""
|
||||
if not is_diagonally_dominant(matrix):
|
||||
raise ValueError("Matrix is not diagonally dominant, Gauss-Seidel may not converge.")
|
||||
raise ValueError(
|
||||
"Matrix is not diagonally dominant, Gauss-Seidel may not converge."
|
||||
)
|
||||
|
||||
n = len(matrix)
|
||||
x = initial_guess.copy()
|
||||
@ -52,6 +55,8 @@ def gauss_seidel(matrix, vector, initial_guess, max_iterations=100, tol=1e-6):
|
||||
|
||||
return x
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
@ -20,6 +20,7 @@ def commutative_law_and(a, b):
|
||||
"""
|
||||
return a and b
|
||||
|
||||
|
||||
def commutative_law_or(a, b):
|
||||
"""
|
||||
Implement the commutative law for OR: A OR B = B OR A.
|
||||
@ -42,8 +43,10 @@ def commutative_law_or(a, b):
|
||||
"""
|
||||
return a or b
|
||||
|
||||
|
||||
# Implement other laws similarly
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
@ -1,5 +1,6 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
def decode_resistor_colors(colors: List[str]) -> float:
|
||||
"""
|
||||
Calculate the resistance value of a resistor based on its color bands.
|
||||
@ -32,9 +33,18 @@ def decode_resistor_colors(colors: List[str]) -> float:
|
||||
raise ValueError("A resistor must have at least three color bands.")
|
||||
|
||||
color_values = {
|
||||
"black": 0, "brown": 1, "red": 2, "orange": 3, "yellow": 4,
|
||||
"green": 5, "blue": 6, "violet": 7, "gray": 8, "white": 9,
|
||||
"gold": 0.1, "silver": 0.01
|
||||
"black": 0,
|
||||
"brown": 1,
|
||||
"red": 2,
|
||||
"orange": 3,
|
||||
"yellow": 4,
|
||||
"green": 5,
|
||||
"blue": 6,
|
||||
"violet": 7,
|
||||
"gray": 8,
|
||||
"white": 9,
|
||||
"gold": 0.1,
|
||||
"silver": 0.01,
|
||||
}
|
||||
|
||||
first_band_value = color_values.get(colors[0].lower(), None)
|
||||
@ -48,6 +58,7 @@ def decode_resistor_colors(colors: List[str]) -> float:
|
||||
|
||||
return resistance
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
from typing import List, Tuple
|
||||
|
||||
|
||||
def calculate_equivalent_emf_and_resistance(
|
||||
cells: List[Tuple[float, float]], connection_type: str
|
||||
) -> Tuple[float, float]:
|
||||
@ -38,6 +39,7 @@ def calculate_equivalent_emf_and_resistance(
|
||||
|
||||
return total_emf, total_internal_resistance
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""
|
||||
Main function to calculate equivalent EMF and internal resistance of cells.
|
||||
@ -47,14 +49,14 @@ def main() -> None:
|
||||
>>> import sys
|
||||
>>> sys.stdin = io.StringIO('3\\n1.5\\n0.2\\n2.0\\n0.3\\n1.0\\n0.1\\n2\\n')
|
||||
>>> main()
|
||||
Enter the number of cells:
|
||||
Enter the EMF of cell 1:
|
||||
Enter the internal resistance of cell 1:
|
||||
Enter the EMF of cell 2:
|
||||
Enter the internal resistance of cell 2:
|
||||
Enter the EMF of cell 3:
|
||||
Enter the internal resistance of cell 3:
|
||||
Select the connection type (1/2):
|
||||
Enter the number of cells:
|
||||
Enter the EMF of cell 1:
|
||||
Enter the internal resistance of cell 1:
|
||||
Enter the EMF of cell 2:
|
||||
Enter the internal resistance of cell 2:
|
||||
Enter the EMF of cell 3:
|
||||
Enter the internal resistance of cell 3:
|
||||
Select the connection type (1/2):
|
||||
Invalid choice. Please select 1 or 2.
|
||||
Equivalent Cell Parameters:
|
||||
Equivalent EMF: 4.5 volts
|
||||
@ -85,7 +87,9 @@ def main() -> None:
|
||||
raise ValueError("Invalid choice. Please select 1 or 2.")
|
||||
|
||||
# Calculate equivalent EMF and internal resistance
|
||||
eq_emf, eq_int_res = calculate_equivalent_emf_and_resistance(cells, connection_type)
|
||||
eq_emf, eq_int_res = calculate_equivalent_emf_and_resistance(
|
||||
cells, connection_type
|
||||
)
|
||||
|
||||
# Output equivalent cell parameters
|
||||
print("\nEquivalent Cell Parameters:")
|
||||
@ -95,6 +99,7 @@ def main() -> None:
|
||||
except ValueError as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
|
@ -1,8 +1,9 @@
|
||||
from typing import List, Tuple
|
||||
|
||||
|
||||
def calculate_electric_field(
|
||||
charges: List[Tuple[float, Tuple[float, float, float]]],
|
||||
point: Tuple[float, float, float]
|
||||
point: Tuple[float, float, float],
|
||||
) -> Tuple[float, Tuple[float, float, float]]:
|
||||
"""
|
||||
Calculate the electric field and potential at a given point due to charges.
|
||||
@ -43,18 +44,21 @@ def calculate_electric_field(
|
||||
dx, dy, dz = (
|
||||
point[0] - charge_position[0],
|
||||
point[1] - charge_position[1],
|
||||
point[2] - charge_position[2]
|
||||
point[2] - charge_position[2],
|
||||
)
|
||||
r = (dx**2 + dy**2 + dz**2)**0.5
|
||||
r = (dx**2 + dy**2 + dz**2) ** 0.5
|
||||
|
||||
electric_field[0] += k * charge_magnitude * dx / r**3
|
||||
electric_field[1] += k * charge_magnitude * dy / r**3
|
||||
electric_field[2] += k * charge_magnitude * dz / r**3
|
||||
|
||||
electric_field_magnitude = (electric_field[0]**2 + electric_field[1]**2 + electric_field[2]**2)**0.5
|
||||
electric_field_magnitude = (
|
||||
electric_field[0] ** 2 + electric_field[1] ** 2 + electric_field[2] ** 2
|
||||
) ** 0.5
|
||||
|
||||
return electric_field_magnitude, tuple(electric_field)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""
|
||||
Main function to calculate electric field and potential at a given point.
|
||||
@ -64,13 +68,27 @@ def main() -> None:
|
||||
charges = []
|
||||
|
||||
for i in range(n):
|
||||
charge_magnitude = float(input(f"Enter charge magnitude (in Coulombs) for charge {i+1}: "))
|
||||
position = tuple(float(coord) for coord in input(f"Enter position (x, y, z) for charge {i+1} (comma-separated): ").split(','))
|
||||
charge_magnitude = float(
|
||||
input(f"Enter charge magnitude (in Coulombs) for charge {i+1}: ")
|
||||
)
|
||||
position = tuple(
|
||||
float(coord)
|
||||
for coord in input(
|
||||
f"Enter position (x, y, z) for charge {i+1} (comma-separated): "
|
||||
).split(",")
|
||||
)
|
||||
charges.append((charge_magnitude, position))
|
||||
|
||||
point = tuple(float(coord) for coord in input("Enter the point (x, y, z) where electric field and potential are to be calculated (comma-separated): ").split(','))
|
||||
point = tuple(
|
||||
float(coord)
|
||||
for coord in input(
|
||||
"Enter the point (x, y, z) where electric field and potential are to be calculated (comma-separated): "
|
||||
).split(",")
|
||||
)
|
||||
|
||||
electric_field_magnitude, electric_field_vector = calculate_electric_field(charges, point)
|
||||
electric_field_magnitude, electric_field_vector = calculate_electric_field(
|
||||
charges, point
|
||||
)
|
||||
|
||||
print("\nElectric Field and Potential at the Given Point:")
|
||||
print(f"Electric Field Magnitude: {electric_field_magnitude} N/C")
|
||||
@ -79,6 +97,7 @@ def main() -> None:
|
||||
except ValueError as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user