计算4频段彩色编码电阻器的电阻


29

电阻器通常具有色码带,用于标识其电阻(欧姆为单位)。在此挑战中,我们将仅考虑常规的4频段,棕褐色,轴向引线电阻器。我们将它们表示为:

xyzt

其中x,第一个有效数字的第一条带y是,第二个有效数字z的第二条带,乘数t的第三条带,公差的第四条带。

每个xyzt代表一个字母,代表乐队的颜色:

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

因此,例如NKOg一些特定的电阻器。

可以借助此表来计算电阻:

电阻色码表

如表所示:

  • x并且y可以是任何字母除外gs_
  • z除了可以是任何东西_
  • 我们会限制t仅是gs_

这是一个方便的电阻计算器,处理与我们完全相同的一组电阻器。

电阻10 * x + yz倍频,给的公差t百分比。

例如,计算的电阻NKOg,我们看到:

  1. N 表示布朗代表1。
  2. K 表示黑色为0。
  3. O表示橙色10 3
  4. g 表示±5%的金。

因此电阻为(10*1 + 0)*10^310000 Ω ±5%

挑战

编写一个程序或函数,该程序或函数接受格式为4的字符串并以格式xyzt打印或返回电阻[resistance] Ω ±[tolerance]%

  • 电阻器可以是“倒置的”,即以相反的顺序tzyx。例如,NKOggOKN都应产生10000 Ω ±5%
  • 电阻始终以普通欧姆为单位,从不以千欧,兆欧等为单位。
  • Ω可以被替换ohms,例如10000 ohms ±5%
  • ±可以被替换+/-,例如10000 Ω +/-5%
  • 在小数点右边加上尾随零是可以的。(例如10000.0 Ω +/-5%
  • 您可以假定输入始终是有效的(x并且y永不gs_z从不_tgs_)。
  • 即使在现实生活中没有产生某些色带组合,也需要支持所有10×10×12×3 = 3600个可能的电阻(2×3600个可能的输入)。

以字节为单位的最短代码获胜。

例子

  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%

当且仅当你喜欢我的挑战,考虑检查出砌块建筑成群的僵尸!

Answers:



9

CJam,53 51 50字节

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

在线尝试

(感谢@ user23013提供了一个字节)


我开始使用Python,但是

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

太贵了...


2
:(2/'e*s~保存[
jimmy23013 2015年

@ user23013啊,谢谢,我一直在尝试了一堆插入的方式e它是必要的,但我从来没有想到/*
SP3000

4

Python 3中,130个 114字节

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)

编辑: @ Sp3000指出,使用min(v,v[::-1])而不是v[::(1,-1)[v[0]in'sg_']](节省10个字节)可以更好地检测到排序,而不是检查的索引_并删除一些不必要的空格。

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)

谢谢-我意识到要串联起来,但是错过了使用min()检测正确顺序的技巧-很好。
慢性病

3

Perl,93个字节

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

1

Haskell,135132130字节

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

多亏了nimi,我才节省了2个字节。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.