33
\$\begingroup\$

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:

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%.

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, 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.

Examples

  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!

\$\endgroup\$
0

7 Answers 7

10
\$\begingroup\$

CJam, 59 58 56 50 bytes

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

Try it online in the CJam interpreter.

\$\endgroup\$
9
\$\begingroup\$

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...

\$\endgroup\$
2
  • 2
    \$\begingroup\$ :(2/'e*s~ saves the [. \$\endgroup\$ 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\$ Commented May 26, 2015 at 5:39
4
\$\begingroup\$

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) 
\$\endgroup\$
1
  • \$\begingroup\$ Thanks - I realised about concatenating the lines, but I missed the trick of using min() to detect the correct ordering - nice. \$\endgroup\$ Commented May 26, 2015 at 15:04
3
\$\begingroup\$

Perl, 93 bytes

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

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)++"%" 

Explanation:

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.

\$\endgroup\$
0
1
\$\begingroup\$

JavaScript (ES6), 90 bytes

s=>+((g=n=>"sgKNROYGBVAW".search(s[n^=s>{}&&3])-2)``+(g(1)+'e')+g(2))+` Ω ±${5<<~g(3)}%` 

Try it online!

Commented

s => // s = input string +( // force to an integer: ( g = n => // g is a helper function taking n: "sgKNROYGBVAW" // color lookup string .search( // search the index of: s[ // the N-th character in s n ^= // where N is either n s > {} // or n XOR 3 (i.e. 2 - n) if the first && 3 // character of s is '_', 'g' or 's' ] // ) - 2 // end of search(), subtract 2 )`` + // initial call to g with n zero'ish (g(1) + 'e') + // 2nd call to g with n = 1, // followed by 'e' for the exponent g(2) // 3rd call to g with n = 2 ) + // this gives the resistance value ` Ω ±` + // append the ohm and +/- symbols `${ // append the tolerance, which is: 5 << ~g(3) // 5 << -1 - g(3) }%` // append the final '%' 
\$\endgroup\$
0
\$\begingroup\$

APL(NARS), 83 chars

{(a b c d)←{⍵[4]>↑⍵:⌽⍵⋄⍵}¯4+'_sgKNROYGBVAW'⍳⍵⋄((10*c)×b+a×10),'Ω ±','%',⍨⍕5×2*∣1+d} 

test:

 f←{(a b c d)←{⍵[4]>↑⍵:⌽⍵⋄⍵}¯4+'_sgKNROYGBVAW'⍳⍵⋄((10*c)×b+a×10),'Ω ±','%',⍨⍕5×2*∣1+d} f '_RYR' 2400 Ω ±20% f 'gBBB' 66000000 Ω ±5% f '_WAV' 78000000000 Ω ±20% f 'GOOs' 53000 Ω ±10% f 'ssGO' 0.35 Ω ±10% f 'ggGO' 3.5 Ω ±5% f 'ggKN' 1 Ω ±5% f 'sOKN' 10000 Ω ±10% f 'KKR_' 0 Ω ±20% f 'sOKN' 10000 Ω ±10% f 'gOKN' 10000 Ω ±5% 
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.