28 lines
706 B
Python
28 lines
706 B
Python
#!/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
|