blob: 630720e9cc72f48b594d5e078a6f03d4dbfbcc28 (
plain)
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
54
55
56
|
#!/bin/python3
from sys import stdin
from math import log, floor
MAX_RESISTORS = 2
def expand(input):
input = input.lower()
result = 0.0
multiplier = 1
multipliers = {
'k': 1e3,
'm': 1e6
}
if not input[-1].isdigit():
multiplier = multipliers.get(input[-1]) or 1
input = input[:-1]
result = float(input)
return result * multiplier
def get_available():
with open("./available", 'r') as file:
content = file.readlines()
return [expand(line.strip()) for line in content]
def calc_resistors(target, available):
resistors = []
for x in range(MAX_RESISTORS):
for resistor in available:
if resistor > target: continue
target -= resistor
resistors.append(resistor)
break
return resistors
def format_resistor_value(num):
multipliers = ['', 'k', 'm']
multiplier = floor(log(num, 1e3))
return f"{(num / (1e3 ** multiplier)):.1f}{multipliers[multiplier]}"
def pretty_print(values, target):
pretty_values = [format_resistor_value(res) for res in values]
print(f"{' + '.join(pretty_values)} = {sum(values):.1f} Ohm --> off by {(sum(values) - target):.1f}")
def main():
available = get_available()
available.sort(reverse=True)
for line in stdin:
if line == '': break
line = line.strip()
resistors = calc_resistors(float(line), available)
pretty_print(resistors, float(line))
if __name__ == "__main__":
main()
|