forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapacitor_equivalence.py
More file actions
Latest commit
53 lines (44 loc) · 1.56 KB
/
Copy pathcapacitor_equivalence.py
File metadata and controls
53 lines (44 loc) · 1.56 KB
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__ importannotations
defcapacitor_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
forindex, capacitorinenumerate(capacitors):
ifcapacitor<0:
msg=f"Capacitor at index {index} has a negative value!"
raiseValueError(msg)
sum_c+=capacitor
returnsum_c
defcapacitor_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
forindex, capacitorinenumerate(capacitors):
ifcapacitor<=0:
msg=f"Capacitor at index {index} has a negative or zero value!"
raiseValueError(msg)
first_sum+=1/capacitor
return1/first_sum
if__name__=="__main__":
importdoctest
doctest.testmod()