Skip to content

Created equivalent_resistance under Electronics #6782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 30, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions electronics/resistor_equivalence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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!
"""

firstSum = 0.00

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: firstSum

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: firstSum

index = 0
for resistor in resistors:
if resistor <= 0:
raise ValueError(f"Resistor at index {index} has a negative or zero value!")
firstSum += 1 / float(resistor)
index += 1
return 1 / firstSum


def resistor_series(resistors: list[float]) -> float: # Req = R1 + R2 + ... + Rn
"""
This function can 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 = 0.00
index = 0
for resistor in resistors:

sum += resistor
if resistor < 0:
raise ValueError(f"Resistor at index {index} has a negative value!")
index += 1
return sum


if __name__ == "__main__":
import doctest

doctest.testmod()