Resistors commonly have color coded bands that are used to identify their resistance in Ohms. In this challenge we'll only consider the normal 4-band, tan, axial-lead resistors. We'll express them as:
x is the first band for the first significant figure,
y is the second band for the second significant figure,
z the third band for the multiplier, and
t is the fourth band for the tolerance.
xyzt represents a letter that abbreviates the color of the band:
K = Black N = Brown R = Red O = Orange Y = Yellow G = Green B = Blue V = Violet A = Gray W = White g = Gold s = Silver _ = None
So, for example,
NKOg is some particular resistor.
The resistance can be calculated with the help of this table:
As the table suggests:
ycan be any letters except
zcan be anything except
- We'll restrict
tto only be
The resistance is
10 * x + y times the
z multiplier, to a tolerance of the
For example, to calculate the resistance of
NKOg, we see that:
Nmeans Brown for 1.
Kmeans Black for 0.
Omeans Orange for 103.
gmeans Gold for ±5%.
So the resistance is
(10*1 + 0)*10^3→
10000 Ω ±5%.
Write a program or function that takes in a 4 character string of the form
xyzt and prints or returns the resistance in the form
[resistance] Ω ±[tolerance]%.
- The resistor may be "upside-down", i.e. in the reverse order
tzyx. For example, both
10000 Ω ±5%.
- The resistance is always in plain ohms, never kilohms, megohms, etc.
Ωmay be replaced with
10000 ohms ±5%.
±may be replaced with
10000 Ω +/-5%.
- Having trailing zeros to the right of a decimal point is fine. (e.g.
10000.0 Ω +/-5%)
- You can assume input is always valid (
- All 10×10×12×3 = 3600 possible resistors (2×3600 possible inputs) need to be supported even if some color band combinations aren't produced in real life.
The shortest code in bytes wins.
10000 ohms +/-5%
0 Ω +/-20%
1 ohms ±5%
3.5 Ω ±5%
0.350 Ω ±10%
53000 ohms +/-10%
48.0 ohms +/-20%
78000000000 Ω ±20%
66000000.000 ohms ±5%
2400.00 ohms ±20%