Chemical sentences! Decrypting

12

0

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!

N3buchadnezzar

Posted 2015-11-26T14:15:57.433

Reputation: 401

Question was closed 2015-11-27T19:59:01.350

Related. – Martin Ender – 2015-11-26T14:21:31.480

5Borderline dupe – Peter Taylor – 2015-11-26T14:46:31.797

Does the output have to be in lowecase or can it be mixed case (for example FIRe IS FUN or PaPa)? – Level River St – 2015-11-26T22:12:15.677

Mixed cases are fine =) – N3buchadnezzar – 2015-11-27T09:57:05.187

Answers

4

Pyth, 264 bytes

s@L+" "-R0+c"h0helibeb0c0n0o0f0nenamgalsip0s0clark0casctiv0crmnfeconicuzngageassebrkrrbsry0zrnbmotcrurhpdagcdinsnsbtei0xecsbalaceprndpmsmeugdtbdyhoertmybluhftaw0reosirptauhgtlpbbipoatrnfrraacthpau0nppuamcmbkcfesfmmdnolrrfdbsgbhhsmtdsrgcn"2c"uutfl0uuplv0uusuuo"3rz7

orlp

Posted 2015-11-26T14:15:57.433

Reputation: 37 067

1Use packed str? – isaacg – 2015-11-26T18:23:48.410

@isaacg Last time I tried to use it it was all buggy :( – orlp – 2015-11-27T03:44:09.837

TIL that the periodic table contains "clark". – user253751 – 2015-11-27T05:44:46.907

But no "kent" :-( – John Dvorak – 2015-11-27T08:19:35.663

2

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

Ypnypn

Posted 2015-11-26T14:15:57.433

Reputation: 10 485

2

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

CSᵠ

Posted 2015-11-26T14:15:57.433

Reputation: 484

2

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>

user81655

Posted 2015-11-26T14:15:57.433

Reputation: 10 181

1

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

Helco

Posted 2015-11-26T14:15:57.433

Reputation: 148

no need for the var prefix and semicolon ; at the end in codegolf. insta 5 bytes off – CSᵠ – 2015-11-28T21:01:11.577

1

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.

Addison Crump

Posted 2015-11-26T14:15:57.433

Reputation: 10 763

1

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.

Level River St

Posted 2015-11-26T14:15:57.433

Reputation: 22 049

1

JavaScript, 275 273 272 bytes

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

You can try it here : JSFiddle

Awashi

Posted 2015-11-26T14:15:57.433

Reputation: 111

1shave a byte by removing the /g flag, no need when splitting – CSᵠ – 2015-11-28T20:51:58.600