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:
xyzt Where 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.
Each of 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:
xandycan be any letters exceptg,s, and_.zcan be anything except_.- We'll restrict
tto only beg,s, or_.
(Here's a handy resistance calculator that deals with exact same set of resistors we are.)
The resistance is 10 * x + y times the z multiplier, to a tolerance of the t percentage.
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%.
Challenge
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, bothNKOgandgOKNshould produce10000 Ω ±5%. - The resistance is always in plain ohms, never kilohms, megohms, etc.
Ωmay be replaced withohms, e.g.10000 ohms ±5%.±may be replaced with+/-, e.g.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 (
xandynevergs_;znever_;tonlygs_). - 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.
Examples
gOKN→10000 ohms +/-5%KKR_→0 Ω +/-20%ggKN→1 ohms ±5%ggGO→3.5 Ω ±5%ssGO→0.350 Ω ±10%GOOs→53000 ohms +/-10%YAK_→48.0 ohms +/-20%_WAV→78000000000 Ω ±20%gBBB→66000000.000 ohms ±5%_RYR→2400.00 ohms ±20%
Iff you enjoy my challenges, consider checking out Block Building Bot Flocks!
