12
\$\begingroup\$

This question already has an answer here:

Background

You are sitting in chemistry class and your friend keep sending you these strange letters. It is your job to decrypt the messages into words.

This is the inverse of this challenge.

enter image description here

The Challenge

Input: A list or a string of integers from 1 to 118.

Output: The words for each chemical element if possible. Otherwise return False, throw an error, or blank. Note in your program 0 must return a whitespace or newline. Ignore all integers not in range.

Test cases Input 1:

9 53 75

Output 1:

fire

Input 2:

91 120 91

Output 2:

papa

Input 3:

9 53 75 0 53 16 0 9 92 7  

Output 3:

fire is fun

Note: Again no built in functions for generating the periodic system or external libaries. Standard loopholes are not allowed.

Scoring

Submissions will be scored in bytes. I recommend this website to keep track of your byte count, though you can use any counter you like.

This is , so the lowest score wins!

\$\endgroup\$

marked as duplicate by Mego, John Dvorak, intrepidcoder, NinjaBearMonkey, JohnE Nov 27 '15 at 19:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Related. \$\endgroup\$ – Martin Ender Nov 26 '15 at 14:21
  • 5
    \$\begingroup\$ Borderline dupe \$\endgroup\$ – Peter Taylor Nov 26 '15 at 14:46
  • \$\begingroup\$ Does the output have to be in lowecase or can it be mixed case (for example FIRe IS FUN or PaPa)? \$\endgroup\$ – Level River St Nov 26 '15 at 22:12
  • \$\begingroup\$ Mixed cases are fine =) \$\endgroup\$ – N3buchadnezzar Nov 27 '15 at 9:57
4
\$\begingroup\$

Pyth, 264 bytes

s@L+" "-R0+c"h0helibeb0c0n0o0f0nenamgalsip0s0clark0casctiv0crmnfeconicuzngageassebrkrrbsry0zrnbmotcrurhpdagcdinsnsbtei0xecsbalaceprndpmsmeugdtbdyhoertmybluhftaw0reosirptauhgtlpbbipoatrnfrraacthpau0nppuamcmbkcfesfmmdnolrrfdbsgbhhsmtdsrgcn"2c"uutfl0uuplv0uusuuo"3rz7
\$\endgroup\$
  • 1
    \$\begingroup\$ Use packed str? \$\endgroup\$ – isaacg Nov 26 '15 at 18:23
  • \$\begingroup\$ @isaacg Last time I tried to use it it was all buggy :( \$\endgroup\$ – orlp Nov 27 '15 at 3:44
  • \$\begingroup\$ TIL that the periodic table contains "clark". \$\endgroup\$ – immibis Nov 27 '15 at 5:44
  • \$\begingroup\$ But no "kent" :-( \$\endgroup\$ – John Dvorak Nov 27 '15 at 8:19
2
\$\begingroup\$

Par, 261 bytes

` 0h0helibeb0c0n0o0f0nenamgalsip0s0clark0casctiv0crmnfeconicuzngageassebrkrrbsry0zrnbmotcrurhpdagcdinsnsbtei0xecsbalaceprndpmsmeugdtbdyhoertmybluhftaw0reosirptauhgtlpbbipoatrnfrraacthpau0nppuamcmbkcfesfmmdnolrrfdbsgbhhsmtdsrgcn`2%`uutfl0uuplv0uusuuo`3%+l✶´gΣ'0-

One byte is used per character, even Σ, since a specialized encoding is used. See here.

Input is Lisp-style, e.g. (9 53 75 0 53 16 0 9 92 7)

` 0h0hel...n`  ## Elements until Uut
2%             ## Split into pieces of size 2
`uutfl0u...o`  ## Elements from Uut
3%             ## Split into pieces of size 3
+              ## Combine into list of elements
l✶             ## Read array of numbers
´g             ## Find the element at each index
Σ              ## Concatenate
'0-            ## Remove all 0's
\$\endgroup\$
2
\$\begingroup\$

Python 2.x, 295 bytes

Golfed code:

import re;print''.join([re.findall('[A-Z ][a-z]*',' HHeLiBeBCNOFNeNaMgAlSiPSClArKCaScTiVCrMnFeCoNiCuZnGaGeAsSeBrKrRbSrYZrNbMoTcRuRhPdAgCdInSnSbTeIXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaWReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaUNpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCnUutFlUupLvUusUuo')[e]for e in i])

NB: to shave 8 bytes, output for this version is in mixed caps (doesn't break any rules), missing the .lower() ending


387 bytes

Golfed code:

print''.join([' ,h,he,li,be,b,c,n,o,f,ne,na,mg,al,si,p,s,cl,ar,k,ca,sc,ti,v,cr,mn,fe,co,ni,cu,zn,ga,ge,as,se,br,kr,rb,sr,y,zr,nb,mo,tc,ru,rh,pd,ag,cd,in,sn,sb,te,i,xe,cs,ba,la,ce,pr,nd,pm,sm,eu,gd,tb,dy,ho,er,tm,yb,lu,hf,ta,w,re,os,ir,pt,au,hg,tl,pb,bi,po,at,rn,fr,ra,ac,th,pa,u,np,pu,am,cm,bk,cf,es,fm,md,no,lr,rf,db,sg,bh,hs,mt,ds,rg,cn,uut,fl,uup,lv,uus,uuo'.split(',')[e]for e in i])

Input: A list or a string of integers from 1 to 118.

Usage: i=[9,53,75,0,53,16,0,9,92,7]

Output: The words for each chemical element if possible. Otherwise return False, throw an error, or blank. Note in your program 0 must return a whitespace or newline. Ignore all integers not in range.

PS: used 2.x for the print statement without parenthesis

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 347 279 bytes

l=>l.map(n=>" HHeLiBeBCNOFNeNaMgAlSiPSClArKCaScTiVCrMnFeCoNiCuZnGaGeAsSeBrKrRbSrYZrNbMoTcRuRhPdAgCdInSnSbTeIXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaWReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaUNpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCnUutFlUupLvUusUuo".match(/[ A-Z][a-z]*/g)[n]||"").join``

Explanation

Uses @CSᵠ's idea of seperating the element names by capital letters. Converting the output to all upper or lower case would add 14 bytes.

l=>
  l.map(n=>                   // iterate through each number

    " HHeLiBeBCNOFNeNaMgAlSiPSClArKCaScTiVCrMnFeCoNiCuZnGaGeAsSeBrKrRbSrYZrNbMoTcRuRhPdAgCdInSnSbTeIXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaWReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaUNpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCnUutFlUupLvUusUuo"
      .match(/[ A-Z][a-z]*/g) // split the elements by the first letter being upper-case

        [n]                   // add the element name to the output
          ||""                // add nothing if the number was out of range
  ).join``                    // return the output as a string

Test

<input type="text" id="input" value="9 53 75 0 53 16 0 9 92 7" /><button onclick='result.innerHTML=(

l=>l.map(n=>" HHeLiBeBCNOFNeNaMgAlSiPSClArKCaScTiVCrMnFeCoNiCuZnGaGeAsSeBrKrRbSrYZrNbMoTcRuRhPdAgCdInSnSbTeIXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaWReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaUNpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCnUutFlUupLvUusUuo".match(/[ A-Z][a-z]*/g)[n]||"").join``

)(input.value.split(" "))'>Go</button><pre id="result"></pre>

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

ES6, 396 bytes

A very trivial approach:

var C=c=>c.map(a=>(" .h.he.li.be.b.c.n.o.f.ne.na.mg.al.si.p.s.cl.ar.k.ca.sc.ti.v.cr.mn.fe.co.ni.cu.zn.ga.ge.as.se.br.kr.rb.sr.y.zr.nb.mo.tc.ru.rh.pd.ag.cd.in.sn.sb.te.i.xe.cs.ba.la.ce.pr.nd.pm.sm.eu.gd.tb.dy.ho.er.tm.yb.lu.hf.ta.w.re.os.ir.pt.au.hg.tl.pb.bi.po.at.rn.fr.ra.ac.th.pa.u.np.pu.am.cm.bk.cf.es.fm.md.no.lr.rf.db.sg.bh.hs.mt.ds.rg.cn.uut.fl.uup.lv.uus.uuo").split(".")[a]||"").join("");

Test it

\$\endgroup\$
  • \$\begingroup\$ no need for the var prefix and semicolon ; at the end in codegolf. insta 5 bytes off \$\endgroup\$ – CSᵠ Nov 28 '15 at 21:01
1
\$\begingroup\$

Java, 470 bytes

interface B{static void main(String[]a){for(int i=0;i<a.length;i++)System.out.print(" ,h,he,li,be,b,c,n,o,f,ne,na,mg,al,si,p,s,cl,ar,k,ca,sc,ti,v,cr,mn,fe,co,ni,cu,zn,ga,ge,as,se,br,kr,rb,sr,y,zr,nb,mo,tc,ru,rh,pd,ag,cd,in,sn,sb,te,i,xe,cs,ba,la,ce,pr,nd,pm,sm,eu,gd,tb,dy,ho,er,tm,yb,lu,hf,ta,w,re,os,ir,pt,au,hg,tl,pb,bi,po,at,rn,fr,ra,ac,th,pa,u,np,pu,am,cm,bk,cf,es,fm,md,no,lr,rf,db,sg,bh,hs,mt,ds,rg,cn,uut,fl,uup,lv,uus,uuo".split(",")[Integer.parseInt(a[i])]);}}

Accepts input as command line arguments. Uses an interface declaration to shorten class/main declaration. It will definitely error for invalid input. Because Java.

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

Ruby, 282

->a{a.each{|n|" HHeLiBeBCNOFNeNaMgAlSiPSClArKCaScTiVCrMnFeCoNiCuZnGaGeAsSeBrKrRbSrYZrNbMoTcRuRhPdAgCdInSnSbTeIXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaWReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaUNpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCnUutFlUupLvUusUuo".bytes{|j|n-=1-j/96;n==-1&&$><<j.chr}}}

Ungolfed in test program

Each number in turn is assigned to n. We run through the magic string bytewise, decreasing n unless the ASCII code is more than 96. When n is -1 we print a byte from the magic string. As n remains the same until all characters for that element are printed, all characters for that element are printed. Out of range numbers are simply ignored.

f=->a{
  a.each{|n|
    " HHeLiBeBCNOFNeNaMgAlSiPSClArKCaScTiVCrMnFeCoNiCuZnGaGeAsSeBrKrRbSrYZrNbMoTcRuRhPdAgCdInSnSbTeIXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaWReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaUNpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCnUutFlUupLvUusUuo".
    bytes{|j|n-=1-j/96;n==-1&&print(j.chr)}
  }
}


f[[59,8,24,95,49,0,15,92,40,99,0,95,60,0,27,4,0,27,53,9]]

Output

PrOCrAmIn PUZrEs AmNd CoBe CoIF

Mixed case. All lowercase would cost an extra 9. All uppercase would cost an extra 7.

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

JavaScript, 275 273 272 bytes

a=>a.map(b=>" HHeLiBeBCNOFNeNaMgAlSiPSClArKaCaScTiVCrMnFeCoNiCuZnGaGeAsSeBrKrRbSrYZrNbMoTcRuRhPdAgCdInSnSbTeIXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaWReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaUNpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCnUutFlUupLvUusUuo".split(/(?=[A-Z])/)[b]).join``

You can try it here : JSFiddle

\$\endgroup\$
  • 1
    \$\begingroup\$ shave a byte by removing the /g flag, no need when splitting \$\endgroup\$ – CSᵠ Nov 28 '15 at 20:51

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