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:


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:

Resistor color code table

As the table suggests:

  • x and y can be any letters except g, s, and _.
  • z can be anything except _.
  • We'll restrict t to only be g, 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:

  1. N means Brown for 1.
  2. K means Black for 0.
  3. O means Orange for 103.
  4. g means Gold for ±5%.

So the resistance is (10*1 + 0)*10^310000 Ω ±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 NKOg and gOKN should produce 10000 Ω ±5%.
  • The resistance is always in plain ohms, never kilohms, megohms, etc.
  • Ω may be replaced with ohms, 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 (x and y never gs_; z never _; t only gs_).
  • 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.


  1. gOKN10000 ohms +/-5%
  2. KKR_0 Ω +/-20%
  3. ggKN1 ohms ±5%
  4. ggGO3.5 Ω ±5%
  5. ssGO0.350 Ω ±10%
  6. GOOs53000 ohms +/-10%
  7. YAK_48.0 ohms +/-20%
  8. _WAV78000000000 Ω ±20%
  9. gBBB66000000.000 ohms ±5%
  10. _RYR2400.00 ohms ±20%

Iff you enjoy my challenges, consider checking out Block Building Bot Flocks!


5 Answers 5


CJam, 59 58 56 50 bytes

r_W%e>"sgKNROYGBVAW"f#2f-~A*+A@#*" Ω ±"@[KA5]='%

Try it online in the CJam interpreter.


CJam, 53 51 50 bytes

" Ω ±"l_W%e<)iB%5*F-'%@"gKNROYGBVAW"f#:(2/'e*s~o

Try it online.

(Thanks to @user23013 for a byte)

I started out in Python, but

eval("%d%de%d"%tuple("gKNROYGBVAW".find(x)-1for x in L))

was too expensive...

  • 2
    \$\begingroup\$ :(2/'e*s~ saves the [. \$\endgroup\$
    – jimmy23013
    Commented May 26, 2015 at 5:35
  • \$\begingroup\$ @user23013 Ah thanks, I've been trying a bunch of ways of inserting the e where it's necessary, but I never thought of / and * \$\endgroup\$
    – Sp3000
    Commented May 26, 2015 at 5:39

Python 3, 130 114 bytes

def f(v):
 a,b,c,d=["_sgKNROYGBVAW".index(x)-3for x in v[::(1,-1)[v[0]in'sg_']]]
 return "%s Ω ±%s%%"%((10*a+b)*10**c,2.5*2**-d)

edit: @Sp3000 points out that the ordering can be better detected with min(v,v[::-1]) rather than v[::(1,-1)[v[0]in'sg_']] (saving 10 bytes), not check the index of _ and remove some unnecessary whitespace.

def f(v):a,b,c,d=["sgKNROYGBVAW".find(x)-2for x in min(v,v[::-1])];return"%s Ω ±%s%%"%((10*a+b)*10**c,2.5*2**-d)
  • \$\begingroup\$ Thanks - I realised about concatenating the lines, but I missed the trick of using min() to detect the correct ordering - nice. \$\endgroup\$
    – chronitis
    Commented May 26, 2015 at 15:04

Perl, 93 bytes

#!perl -lp
ord>90and$_=reverse;s/./-3+index zsgKNROYGBVAW,$&/ge;$_=s/..\K/e/*$_." Ω ±"./.$/*5*$&."%"

Haskell, 135 132 130 bytes

r y|y<"["=p[k|j<-y,(c,k)<-zip"_ sgKNROYGBVAW"[-4..],c==j]
r y=r.reverse$y
p[a,b,c,d]=show((a*10+b)*10**c)++" Ω ±"++show(-5*d)++"%"


r y|y<"["=            If first letter of argument is a capital
p[..]                 Call p on the list created
[k|                   Make a list of all k
   j<-y               Draw character j from input
       ,(c,k)<-       With (c,k) being a pair from
               zip    A list of pairs of corresponding elements from the lists:
"_ sgKNROYGBVAW"       The space at 2nd position is to match '_' with -4, but 's' with -2
[-4..]                 An infinite list starting at -4
,c==j]                Only use element k if j equals the character c

r y=r.reverse$y       If first call fails, call again with reversed argument.

p[a,b,c,d]=           Assign the first four elements of the argument to a,b,c,d respectively.
show                  Turn (number) into string
10**c                 10 to the power of c
++                    Concatenate strings
-5*d                  This works for the tolerance because '_' makes d=-4

Thanks to nimi, I shaved off another 2 bytes.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.