-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
de06353
Create resistor_equivalence.py
Gmuslow 2a22500
Update resistor_equivalence.py
Gmuslow eea6d60
Update electronics/resistor_equivalence.py
Gmuslow 21d3637
Update resistor_equivalence.py
Gmuslow f960440
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 68af9ca
Update resistor_equivalence.py
Gmuslow 4bb0d90
Update resistor_equivalence.py
cclauss 2b15cec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
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 | ||
""" | ||
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: | ||
|
||
Gmuslow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:first_Sum