Calcola la resistenza di un resistore con codice colore a 4 bande


29

I resistori hanno comunemente bande colorate che vengono utilizzate per identificare la loro resistenza in Ohm . In questa sfida considereremo solo i normali resistori a 4 bande, tan, con cavo assiale. Li esprimeremo come:

xyzt

Dov'è xla prima banda per la prima cifra significativa, yè la seconda banda per la seconda cifra significativa, zla terza banda per il moltiplicatore ed tè la quarta banda per la tolleranza .

Ognuno di xyztrappresenta una lettera che abbrevia il colore della 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

Quindi, per esempio, NKOgc'è un resistore particolare.

La resistenza può essere calcolata con l'aiuto di questa tabella:

Tabella dei codici colore dei resistori

Come suggerisce la tabella:

  • xe ypuò essere qualsiasi lettera ad eccezione g, se _.
  • zpuò essere qualsiasi cosa tranne _.
  • Ci limitiamo tad essere solo g, so _.

( Ecco un pratico calcolatore di resistenza che si occupa esattamente dello stesso set di resistenze che siamo. )

La resistenza è moltiplicata per 10 * x + yil zmoltiplicatore, fino a una tolleranza della tpercentuale.

Ad esempio, per calcolare la resistenza di NKOg, vediamo che:

  1. N significa Brown per 1.
  2. K significa nero per 0.
  3. Osignifica arancione per 10 3 .
  4. g significa oro per ± 5%.

Quindi la resistenza è (10*1 + 0)*10^310000 Ω ±5%.

Sfida

Scrivi un programma o una funzione che accetta una stringa di 4 caratteri del modulo xyzte stampa o restituisce la resistenza nel modulo [resistance] Ω ±[tolerance]%.

  • Il resistore può essere "capovolto", cioè nell'ordine inverso tzyx. Ad esempio, entrambi NKOge gOKNdovrebbero produrre 10000 Ω ±5%.
  • La resistenza è sempre in ohm semplici, mai chilohm, megohms, ecc.
  • Ωpuò essere sostituito con ohms, ad es 10000 ohms ±5%.
  • ±può essere sostituito con +/-, ad es 10000 Ω +/-5%.
  • Avere zeri finali alla destra di un punto decimale va bene. (ad es. 10000.0 Ω +/-5%)
  • Puoi supporre che l'input sia sempre valido ( xe ymai gs_; zmai _; tsolo gs_).
  • Tutti i 10 × 10 × 12 × 3 = 3600 possibili resistori (2 × 3600 possibili ingressi) devono essere supportati anche se alcune combinazioni di bande di colore non sono prodotte nella vita reale.

Vince il codice più breve in byte.

Esempi

  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%

Se ti piacciono le mie sfide, prova a dare un'occhiata a Block Building Bot Flocks!

Risposte:


10

CJam, 59 58 56 50 byte

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

Provalo online nell'interprete CJam .


9

CJam, 53 51 50 byte

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

Provalo online .

(Grazie a @ user23013 per un byte)


Ho iniziato con Python, ma

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

era troppo costoso ...


2
:(2/'e*s~salva il file [.
jimmy23013,

@ user23013 Ah grazie, ho provato un sacco di modi per inserire edove è necessario, ma non ci avevo mai pensato /e*
Sp3000,

4

Python 3, 130 114 byte

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)

modifica: @ Sp3000 sottolinea che è possibile rilevare meglio l'ordinamento min(v,v[::-1])piuttosto che v[::(1,-1)[v[0]in'sg_']](salvando 10 byte), non controllare l'indice _e rimuovere alcuni spazi bianchi non necessari.

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)

Grazie - mi sono reso conto di concatenare le linee, ma mi mancava il trucco di usare min()per rilevare l'ordinamento corretto - bello.
cronite,

3

Perl, 93 byte

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

1

Haskell, 135 132 130 byte

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

Spiegazione:

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

Grazie a nimi, mi sono rasato altri 2 byte.

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.