mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Created equivalent_resistance under Electronics (#6782)
* Create resistor_equivalence.py * Update resistor_equivalence.py * Update electronics/resistor_equivalence.py removed an unnecessary space Co-authored-by: Caeden <caedenperelliharris@gmail.com> * Update resistor_equivalence.py fixed the snake_case requirement * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update resistor_equivalence.py finalize the naming convention errors (hopefully) * Update resistor_equivalence.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Caeden <caedenperelliharris@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
e12516debb
commit
c0168cd33f
58
electronics/resistor_equivalence.py
Normal file
58
electronics/resistor_equivalence.py
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
# https://byjus.com/equivalent-resistance-formula/
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
def resistor_parallel(resistors: list[float]) -> float:
|
||||||
|
"""
|
||||||
|
Req = 1/ (1/R1 + 1/R2 + ... + 1/Rn)
|
||||||
|
|
||||||
|
>>> resistor_parallel([3.21389, 2, 3])
|
||||||
|
0.8737571620498019
|
||||||
|
>>> resistor_parallel([3.21389, 2, -3])
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Resistor at index 2 has a negative or zero value!
|
||||||
|
>>> resistor_parallel([3.21389, 2, 0.000])
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Resistor at index 2 has a negative or zero value!
|
||||||
|
"""
|
||||||
|
|
||||||
|
first_sum = 0.00
|
||||||
|
index = 0
|
||||||
|
for resistor in resistors:
|
||||||
|
if resistor <= 0:
|
||||||
|
raise ValueError(f"Resistor at index {index} has a negative or zero value!")
|
||||||
|
first_sum += 1 / float(resistor)
|
||||||
|
index += 1
|
||||||
|
return 1 / first_sum
|
||||||
|
|
||||||
|
|
||||||
|
def resistor_series(resistors: list[float]) -> float:
|
||||||
|
"""
|
||||||
|
Req = R1 + R2 + ... + Rn
|
||||||
|
|
||||||
|
Calculate the equivalent resistance for any number of resistors in parallel.
|
||||||
|
|
||||||
|
>>> resistor_series([3.21389, 2, 3])
|
||||||
|
8.21389
|
||||||
|
>>> resistor_series([3.21389, 2, -3])
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Resistor at index 2 has a negative value!
|
||||||
|
"""
|
||||||
|
sum_r = 0.00
|
||||||
|
index = 0
|
||||||
|
for resistor in resistors:
|
||||||
|
sum_r += resistor
|
||||||
|
if resistor < 0:
|
||||||
|
raise ValueError(f"Resistor at index {index} has a negative value!")
|
||||||
|
index += 1
|
||||||
|
return sum_r
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user