utils + parallel resistors calculator
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
.notes.txt
|
.notes.txt
|
||||||
|
__pycache__/
|
||||||
|
|||||||
18
parallel_resistors.py
Normal file
18
parallel_resistors.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#!/bin/python
|
||||||
|
from utils import parse_resistors
|
||||||
|
|
||||||
|
def get_input():
|
||||||
|
raw_input_resistors = input("Enter resistor values separated by spaces or commas: ")
|
||||||
|
resistors = parse_resistors(raw_input_resistors)
|
||||||
|
return resistors
|
||||||
|
|
||||||
|
def calculate_resistance(resistors):
|
||||||
|
return 1 / sum(1 / r for r in resistors)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
try:
|
||||||
|
resistors = get_input()
|
||||||
|
except ValueError as e:
|
||||||
|
print(e)
|
||||||
|
exit(1)
|
||||||
|
print(f"Total resistance = {calculate_resistance(resistors)} ohms")
|
||||||
27
utils.py
Normal file
27
utils.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#!/bin/python
|
||||||
|
import re
|
||||||
|
|
||||||
|
def parse_resistors(raw_input):
|
||||||
|
values = []
|
||||||
|
tokens = raw_input.replace(',', ' ').split()
|
||||||
|
|
||||||
|
for token in tokens:
|
||||||
|
match = re.fullmatch(r'([0-9]*\.?[0-9]+)\s*([KM]?)', token)
|
||||||
|
if not match:
|
||||||
|
raise ValueError(f"Invalid component value '{token}'")
|
||||||
|
|
||||||
|
number = float(match.group(1))
|
||||||
|
if number <= 0:
|
||||||
|
raise ValueError(f"Invalid component value '{token}' (must be positive)")
|
||||||
|
|
||||||
|
suffix = match.group(2)
|
||||||
|
if suffix == 'K':
|
||||||
|
multiplier = 1e3
|
||||||
|
elif suffix == 'M':
|
||||||
|
multiplier = 1e6
|
||||||
|
else:
|
||||||
|
multiplier = 1.0
|
||||||
|
|
||||||
|
values.append(number * multiplier)
|
||||||
|
|
||||||
|
return values
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
#!/bin/python
|
#!/bin/python
|
||||||
import re
|
from utils import parse_resistors
|
||||||
|
|
||||||
def find_bests(desired_ratio, resistors):
|
def find_bests(desired_ratio, resistors):
|
||||||
# only keep the 5 best candidates
|
# only keep the 5 best candidates
|
||||||
@@ -22,31 +22,6 @@ def find_bests(desired_ratio, resistors):
|
|||||||
for i, (error, R1, R2, ratio) in enumerate(candidates, start=1):
|
for i, (error, R1, R2, ratio) in enumerate(candidates, start=1):
|
||||||
print(f"{i}. R1 = {R1} Ω, R2 = {R2} Ω -> ratio = {ratio:.6f}, error = {error:.6e}")
|
print(f"{i}. R1 = {R1} Ω, R2 = {R2} Ω -> ratio = {ratio:.6f}, error = {error:.6e}")
|
||||||
|
|
||||||
def parse_resistors(raw_input):
|
|
||||||
values = []
|
|
||||||
tokens = raw_input.replace(',', ' ').split()
|
|
||||||
|
|
||||||
for token in tokens:
|
|
||||||
match = re.fullmatch(r'([0-9]*\.?[0-9]+)\s*([KM]?)', token)
|
|
||||||
if not match:
|
|
||||||
raise ValueError(f"Invalid component value '{token}'")
|
|
||||||
|
|
||||||
number = float(match.group(1))
|
|
||||||
if number <= 0:
|
|
||||||
raise ValueError(f"Invalid component value '{token}' (must be positive)")
|
|
||||||
|
|
||||||
suffix = match.group(2)
|
|
||||||
if suffix == 'K':
|
|
||||||
multiplier = 1e3
|
|
||||||
elif suffix == 'M':
|
|
||||||
multiplier = 1e6
|
|
||||||
else:
|
|
||||||
multiplier = 1.0
|
|
||||||
|
|
||||||
values.append(number * multiplier)
|
|
||||||
|
|
||||||
return values
|
|
||||||
|
|
||||||
def get_ratio():
|
def get_ratio():
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user