MCPcopy
hub / github.com/TheAlgorithms/Python / electrical_impedance

Function electrical_impedance

electronics/electrical_impedance.py:12–40  ·  view source on GitHub ↗

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: float, reactance: float, impedance: float
)

Source from the content-addressed store, hash-verified

10
11
12def electrical_impedance(
13 resistance: float, reactance: float, impedance: float
14) -> dict[str, float]:
15 """
16 Apply Electrical Impedance formula, on any two given electrical values,
17 which can be resistance, reactance, and impedance, and then in a Python dict
18 return name/value pair of the zero value.
19
20 >>> electrical_impedance(3,4,0)
21 {'impedance': 5.0}
22 >>> electrical_impedance(0,4,5)
23 {'resistance': 3.0}
24 >>> electrical_impedance(3,0,5)
25 {'reactance': 4.0}
26 >>> electrical_impedance(3,4,5)
27 Traceback (most recent call last):
28 ...
29 ValueError: One and only one argument must be 0
30 """
31 if (resistance, reactance, impedance).count(0) != 1:
32 raise ValueError("One and only one argument must be 0")
33 if resistance == 0:
34 return {"resistance": sqrt(pow(impedance, 2) - pow(reactance, 2))}
35 elif reactance == 0:
36 return {"reactance": sqrt(pow(impedance, 2) - pow(resistance, 2))}
37 elif impedance == 0:
38 return {"impedance": sqrt(pow(resistance, 2) + pow(reactance, 2))}
39 else:
40 raise ValueError("Exactly one argument must be 0")
41
42
43if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

countMethod · 0.80

Tested by

no test coverage detected