| Bytes | Lang | Time | Link |
|---|---|---|---|
| 090 | JavaScript ES6 | 250909T215918Z | Arnauld |
| 083 | APLNARS | 250910T071409Z | Rosario |
| 130 | Haskell | 150528T155325Z | AplusKmi |
| 114 | Python 3 | 150526T102456Z | chroniti |
| 093 | Perl | 150526T132100Z | nutki |
| 050 | CJam | 150526T043346Z | Sp3000 |
| 050 | CJam | 150526T035819Z | Dennis |
JavaScript (ES6), 90 bytes
s=>+((g=n=>"sgKNROYGBVAW".search(s[n^=s>{}&&3])-2)``+(g(1)+'e')+g(2))+` Ω ±${5<<~g(3)}%`
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 '%'
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%
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.
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)
Perl, 93 bytes
#!perl -lp
ord>90and$_=reverse;s/./-3+index zsgKNROYGBVAW,$&/ge;$_=s/..\K/e/*$_." Ω ±"./.$/*5*$&."%"
CJam, 53 51 50 bytes
" Ω ±"l_W%e<)iB%5*F-'%@"gKNROYGBVAW"f#:(2/'e*s~o
(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...
CJam, 59 58 56 50 bytes
r_W%e>"sgKNROYGBVAW"f#2f-~A*+A@#*" Ω ±"@[KA5]='%
Try it online in the CJam interpreter.