From 99053c4727b99edcc5ddc1c1a950df071b98cee0 Mon Sep 17 00:00:00 2001 From: Mary-0165 <146911989+Mary-0165@users.noreply.github.com> Date: Thu, 5 Oct 2023 13:16:11 +0000 Subject: [PATCH 1/4] capacitor equivalence algorithm --- electronics/capacitor_equivalence.py | 57 ++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 electronics/capacitor_equivalence.py diff --git a/electronics/capacitor_equivalence.py b/electronics/capacitor_equivalence.py new file mode 100644 index 000000000000..3c89b6fc977c --- /dev/null +++ b/electronics/capacitor_equivalence.py @@ -0,0 +1,57 @@ +# https://farside.ph.utexas.edu/teaching/316/lectures/node46.html + +from __future__ import annotations + + +def capacitor_parallel(capacitors: list[float]) -> float: + """ + Ceq = C1 + C2 + ... + Cn + Calculate the equivalent resistance for any number of capacitors in parallel. + >>> capacitor_parallel([5.71389, 12, 3]) + 20.71389 + >>> capacitor_parallel([5.71389, 12, -3]) + Traceback (most recent call last): + ... + ValueError: Capacitor at index 2 has a negative value! + """ + sum_c = 0.00 + index = 0 + for capacitor in capacitors: + sum_c += capacitor + if capacitor < 0: + msg = f"Capacitor at index {index} has a negative value!" + raise ValueError(msg) + index += 1 + return sum_c + + +def capacitor_series(capacitors: list[float]) -> float: + """ + Ceq = 1/ (1/C1 + 1/C2 + ... + 1/Cn) + >>> capacitor_series([5.71389,12,3]) + 1.6901062252507735 + >>> capacitor_series([5.71389,12, -3]) + Traceback (most recent call last): + ... + ValueError: Capacitor at index 2 has a negative or zero value! + >>> capacitor_series([5.71389,12, 0.000]) + Traceback (most recent call last): + ... + ValueError: Capacitor at index 2 has a negative or zero value! + """ + + first_sum = 0.00 + index = 0 + for capacitor in capacitors: + if capacitor <= 0: + msg = f"Capacitor at index {index} has a negative or zero value!" + raise ValueError(msg) + first_sum += 1 / float(capacitor) + index += 1 + return 1 / first_sum + + +if __name__ == "__main__": + import doctest + + doctest.testmod() \ No newline at end of file From 9e69f5aef2c83ee03fe5253bb09e12e314e707c0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 13:20:30 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- electronics/capacitor_equivalence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/electronics/capacitor_equivalence.py b/electronics/capacitor_equivalence.py index 3c89b6fc977c..6fee43a9b4f1 100644 --- a/electronics/capacitor_equivalence.py +++ b/electronics/capacitor_equivalence.py @@ -54,4 +54,4 @@ def capacitor_series(capacitors: list[float]) -> float: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From d63274d4b797514a33bab39576debedc43cac074 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Thu, 26 Oct 2023 04:04:19 -0400 Subject: [PATCH 3/4] Apply suggestions from code review --- electronics/capacitor_equivalence.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/electronics/capacitor_equivalence.py b/electronics/capacitor_equivalence.py index 6fee43a9b4f1..5540c52d653e 100644 --- a/electronics/capacitor_equivalence.py +++ b/electronics/capacitor_equivalence.py @@ -14,40 +14,36 @@ def capacitor_parallel(capacitors: list[float]) -> float: ... ValueError: Capacitor at index 2 has a negative value! """ - sum_c = 0.00 - index = 0 - for capacitor in capacitors: - sum_c += capacitor + sum_c = 0 + for index, capacitor in enumerate(capacitors): if capacitor < 0: msg = f"Capacitor at index {index} has a negative value!" raise ValueError(msg) - index += 1 + sum_c += capacitor return sum_c def capacitor_series(capacitors: list[float]) -> float: """ Ceq = 1/ (1/C1 + 1/C2 + ... + 1/Cn) - >>> capacitor_series([5.71389,12,3]) + >>> capacitor_series([5.71389, 12, 3]) 1.6901062252507735 - >>> capacitor_series([5.71389,12, -3]) + >>> capacitor_series([5.71389, 12, -3]) Traceback (most recent call last): ... ValueError: Capacitor at index 2 has a negative or zero value! - >>> capacitor_series([5.71389,12, 0.000]) + >>> capacitor_series([5.71389, 12, 0.000]) Traceback (most recent call last): ... ValueError: Capacitor at index 2 has a negative or zero value! """ - first_sum = 0.00 - index = 0 - for capacitor in capacitors: + first_sum = 0 + for index, capacitor in enumerate(capacitors): if capacitor <= 0: msg = f"Capacitor at index {index} has a negative or zero value!" raise ValueError(msg) - first_sum += 1 / float(capacitor) - index += 1 + first_sum += 1 / capacitor return 1 / first_sum From b94f433725d8a7b414335a69710c6411ace07e15 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Thu, 26 Oct 2023 04:06:30 -0400 Subject: [PATCH 4/4] Update capacitor_equivalence.py --- electronics/capacitor_equivalence.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/electronics/capacitor_equivalence.py b/electronics/capacitor_equivalence.py index 5540c52d653e..274b18afb3ef 100644 --- a/electronics/capacitor_equivalence.py +++ b/electronics/capacitor_equivalence.py @@ -14,7 +14,7 @@ def capacitor_parallel(capacitors: list[float]) -> float: ... ValueError: Capacitor at index 2 has a negative value! """ - sum_c = 0 + sum_c = 0.0 for index, capacitor in enumerate(capacitors): if capacitor < 0: msg = f"Capacitor at index {index} has a negative value!" @@ -38,7 +38,7 @@ def capacitor_series(capacitors: list[float]) -> float: ValueError: Capacitor at index 2 has a negative or zero value! """ - first_sum = 0 + first_sum = 0.0 for index, capacitor in enumerate(capacitors): if capacitor <= 0: msg = f"Capacitor at index {index} has a negative or zero value!"