TheAlgorithms-Python
41 строка · 1.3 Кб
1# https://en.wikipedia.org/wiki/Wheatstone_bridge
2from __future__ import annotations3
4
5def wheatstone_solver(6resistance_1: float, resistance_2: float, resistance_3: float7) -> float:8"""9This function can calculate the unknown resistance in an wheatstone network,
10given that the three other resistances in the network are known.
11The formula to calculate the same is:
12
13---------------
14|Rx=(R2/R1)*R3|
15---------------
16
17Usage examples:
18>>> wheatstone_solver(resistance_1=2, resistance_2=4, resistance_3=5)
1910.0
20>>> wheatstone_solver(resistance_1=356, resistance_2=234, resistance_3=976)
21641.5280898876405
22>>> wheatstone_solver(resistance_1=2, resistance_2=-1, resistance_3=2)
23Traceback (most recent call last):
24...
25ValueError: All resistance values must be positive
26>>> wheatstone_solver(resistance_1=0, resistance_2=0, resistance_3=2)
27Traceback (most recent call last):
28...
29ValueError: All resistance values must be positive
30"""
31
32if resistance_1 <= 0 or resistance_2 <= 0 or resistance_3 <= 0:33raise ValueError("All resistance values must be positive")34else:35return float((resistance_2 / resistance_1) * resistance_3)36
37
38if __name__ == "__main__":39import doctest40
41doctest.testmod()42