forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capacitor_equivalence.py
53 lines (44 loc) · 1.56 KB
/
capacitor_equivalence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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.0
for index, capacitor in enumerate(capacitors):
if capacitor < 0:
msg = f"Capacitor at index {index} has a negative value!"
raise ValueError(msg)
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])
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.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 / capacitor
return 1 / first_sum
if __name__ == "__main__":
import doctest
doctest.testmod()