mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 09:10:16 +00:00
fc33c50593
updates: - [github.com/astral-sh/ruff-pre-commit: v0.4.10 → v0.5.0](https://github.com/astral-sh/ruff-pre-commit/compare/v0.4.10...v0.5.0) - [github.com/pre-commit/mirrors-mypy: v1.10.0 → v1.10.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.10.0...v1.10.1) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
"""Electrical impedance is the measure of the opposition that a
|
|
circuit presents to a current when a voltage is applied.
|
|
Impedance extends the concept of resistance to alternating current (AC) circuits.
|
|
Source: https://en.wikipedia.org/wiki/Electrical_impedance
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from math import pow, sqrt # noqa: A004
|
|
|
|
|
|
def electrical_impedance(
|
|
resistance: float, reactance: float, impedance: float
|
|
) -> dict[str, float]:
|
|
"""
|
|
Apply Electrical Impedance formula, on any two given electrical values,
|
|
which can be resistance, reactance, and impedance, and then in a Python dict
|
|
return name/value pair of the zero value.
|
|
|
|
>>> electrical_impedance(3,4,0)
|
|
{'impedance': 5.0}
|
|
>>> electrical_impedance(0,4,5)
|
|
{'resistance': 3.0}
|
|
>>> electrical_impedance(3,0,5)
|
|
{'reactance': 4.0}
|
|
>>> electrical_impedance(3,4,5)
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: One and only one argument must be 0
|
|
"""
|
|
if (resistance, reactance, impedance).count(0) != 1:
|
|
raise ValueError("One and only one argument must be 0")
|
|
if resistance == 0:
|
|
return {"resistance": sqrt(pow(impedance, 2) - pow(reactance, 2))}
|
|
elif reactance == 0:
|
|
return {"reactance": sqrt(pow(impedance, 2) - pow(resistance, 2))}
|
|
elif impedance == 0:
|
|
return {"impedance": sqrt(pow(resistance, 2) + pow(reactance, 2))}
|
|
else:
|
|
raise ValueError("Exactly one argument must be 0")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|