# Secret Message Part 1, Elements

You and your friend want to send each other secret messages. However, because you are conspiracy theorists and think that the government has a quantum computer that can crack any standard encryption. Therefore, you are inventing one of your own. The first step of this is as follows: taking an input string, you check if all the letters can be represented by the symbols for the elements of the periodic table (case insensitive). If they can be, you replace each section with the name of the element the symbol represents. If all letters cannot be replaced in this way, you simply use the original string.

You are to write a program or function that encodes a message, as outlined previously. Remember that if your program fetches data from an outside source, the size of the outside source must be added to the byte count (this loophole). The elements and symbols used are here:

H   Hydrogen
He  Helium
Li  Lithium
Be  Beryllium
B   Boron
C   Carbon
N   Nitrogen
O   Oxygen
F   Fluorine
Ne  Neon
Na  Sodium
Mg  Magnesium
Al  Aluminum
Si  Silicon
P   Phosphorus
S   Sulfur
Cl  Chlorine
Ar  Argon
K   Potassium
Ca  Calcium
Sc  Scandium
Ti  Titanium
Cr  Chromium
Mn  Manganese
Fe  Iron
Co  Cobalt
Ni  Nickel
Cu  Copper
Zn  Zinc
Ga  Gallium
Ge  Germanium
As  Arsenic
Se  Selenium
Br  Bromine
Kr  Krypton
Rb  Rubidium
Sr  Strontium
Y   Yttrium
Zr  Zirconium
Nb  Niobium
Mo  Molybdenum
Tc  Technetium
Ru  Ruthenium
Rh  Rhodium
Ag  Silver
In  Indium
Sn  Tin
Sb  Antimony
Te  Tellurium
I   Iodine
Xe  Xenon
Cs  Cesium
Ba  Barium
La  Lanthanum
Ce  Cerium
Pr  Praseodymium
Nd  Neodymium
Pm  Promethium
Sm  Samarium
Eu  Europium
Tb  Terbium
Dy  Dysprosium
Ho  Holmium
Er  Erbium
Tm  Thulium
Yb  Ytterbium
Lu  Lutetium
Hf  Hafnium
Ta  Tantalum
W   Tungsten
Re  Rhenium
Os  Osmium
Ir  Iridium
Pt  Platinum
Au  Gold
Hg  Mercury
Tl  Thallium
Bi  Bismuth
Po  Polonium
At  Astatine
Fr  Francium
Ac  Actinium
Th  Thorium
Pa  Protactinium
U   Uranium
Np  Neptunium
Pu  Plutonium
Am  Americium
Cm  Curium
Bk  Berkelium
Cf  Californium
Es  Einsteinium
Fm  Fermium
Md  Mendelevium
No  Nobelium
Lr  Lawrencium
Rf  Rutherfordium
Db  Dubnium
Sg  Seaborgium
Bh  Bohrium
Hs  Hassium
Mt  Meitnerium
Rg  Roentgenium
Cn  Copernicium
Nh  Nihonium
Fl  Flerovium
Mc  Moscovium
Lv  Livermorium
Ts  Tennessine
Og  Oganesson


## Input:

A string to be encoded. You may take this in all caps or lowercase if you wish, as long as you specify that requirement in your answer.

## Output:

The string, encoded as outlined previously if possible.

## Examples:

Hi!                --> HydrogenIodine!
This is an example --> This is an example
Neon               --> NeonOxygenNitrogen
Snip               --> SulfurNitrogenIodinePhosphorus OR TinIodinePhosphorus
Nag                --> NitrogenSilver


## Scoring:

This is , shortest code in bytes wins!

• Comments are not for extended discussion; this conversation has been moved to chat. – Dennis Jul 28 '17 at 16:17
• Can everyone just take the elements and symbols as a dictionary as input because as of now, this challenge is basically all about compressing that? – Daniel Jul 29 '17 at 0:00
• @Dopapp, no, as that would make the answers that have already been submitted, and that people have worked very hard on, completely uncompetitive. Compressing the elements was always going to be most of the challenge anyway. – Gryphon Jul 29 '17 at 11:10

# Mathematica, 404 (239) bytes

S="";l=ToLowerCase;e=ElementData;Unprotect[e];a="Abbreviation";n="Name";e[113,a]="Nh";e["Nh",n]="Nihonium";e[115,a]="Mc";e["Mc",n]="Moscovium";e[117,a]="Ts";e["Ts",n]="Tennessine";e[118,a]="Og";e["Og",n]="Oganesson";r=StringReplace;A=Reverse@SortBy[Table[e[j,a],{j,118}],StringLength];If[StringFreeQ[r[l@S,Table[l@A[[j]]->"",{j,118}]],Alphabet[]],r[l@S,Table[l@A[[j]]->Capitalize@e[A[[j]],n],{j,118}]],S]


Using Mathematica's built-in database to fetch element names and their abbreviation. Input can be mixed lower and upper case and is stored in the variable S. The output is the expression's result, it is not explicitly printed.

The at this time fully working code takes up 404 bytes, as Mathematica's built-in chemical database is a bit behind. ElementData[118, "Name"] returns ununoctium instead of oganesson (the super-heavy elements where only recently properly named, ununoctium was a placeholder name for element 118).
To update ElementData, I unprotect it and fix the values for elements Nihonium (113), Moscovium (115), Tennessine (118) and Oganesson (118).

If Mathematica's database were up to date, I would only require 239 bytes.

S="";l=ToLowerCase;e=ElementData;r=StringReplace;A=Reverse@SortBy[Table[e[j,"Abbreviation"],{j,118}],StringLength];If[StringFreeQ[r[l@S,Table[l@A[[j]]->"",{j,118}]],Alphabet[]],r[l@S,Table[l@A[[j]]->Capitalize@e[A[[j]],"Name"],{j,118}]],S]


## JavaScript (ES6), 881 871 bytes

Takes the input string in all caps.

s=>(o={},'HEelLIithBEeryllMGagnesCAalcTIitanVanadCRhromGAallGEermanSEelenRBubidSRtrontYttrZRirconTCechnetRUuthenRHhodPDalladCDadmTEellurBAarCEerPRraseodymNDeodymPMromethSMamarEUuropGDadolinTBerbDYysprosERrbTMhulLUutetREhenIRridTLhallFRrancRAadACctinTHhorPArotactinUranAMmericCMurESinsteinFMermMDendelevLRawrencRFutherfordDBubnSGeaborgMTeitnerDSarmstadtRGoentgenFLlerovMCoscovLVivermorHydrogenBoronCarbonNitrogenOxygenFluorineNEeonALluminumPhosphorusSulfurCLhlorineARrgonMNanganeseZNincASrsenicBRromineKRryptonMOolybdenumIodineXEenonLAanthanumTAantalumPTlatinumATstatineRNadonTSennessineOGganessonNAsodiumKpotassiumFEironAGsilverWtungstenAUgoldHGmercury'.split(/([A-Z]+|[a-z]+)/).map((r,i,a)=>i%4-3?0:o[e=a[i-2]]=i>339?r[0].toUpperCase()+r.slice(1):e[0]+r+(i<235?'ium':'')),g=([c,...s],r)=>c?c<'A'|c>'Z'?g(s,r+c):o[c]&&g(s,r+o[c])||o[c+=s.shift()]&&g(s,r+o[c]):r)(s,'')||s


### Extended test cases

Because this challenge is also a variant of the exact cover set problem, I've added the following test cases:

• "NA" → "Sodium"
• "NAG" → "N" + "AG" → "NitrogenSilver"
• "NAP" → "NA" + "P" → "SodiumPhosphorus"

let f =

console.log(f('HI!'))
console.log(f('THIS IS AN EXAMPLE'))
console.log(f('NEON'))
console.log(f('SNIP'))
console.log(f('NA'))
console.log(f('NAG'))
console.log(f('NAP'))

### How?

Preliminary optimization

We ignore the following 26 elements entirely, because they can safely be replaced with two symbols of one character among BCFHIKNOPSUVWY:

Bh, Bi, Bk, Cf, Cn, Co, Cs, Cu, Hf, Ho, Hs, In, Nb,
Nh, Ni, No, Np, Os, Pb, Po, Pu, Sb, Sc, Si, Sn, Yb


Encoding and decoding the elements

We use an interlaced list of element symbols in upper case and element names in lower case. Symbols are always stored as-is, while names are shortened according to the following rules:

1. If the first letter of the name matches the first letter of the symbol, we omit it.
2. If the element passes rule #1 and its name ends in "ium", we omit this suffix.

Examples:

• Ag / Silver: "AGsilver"
• K / Potassium: "Kpotassium"
• Zn / Zinc: "ZNinc"
• He / Helium: "HEel"

The 58 elements that trigger both rules are stored at the beginning of the list, followed by the 27 elements that trigger rule #1 only, followed by the 7 elements that do not trigger any rule.

We decode this list to populate the lookup table o, where the keys are the symbols and the values are the decoded element names:

"HEelLIith[...]HGmercury"       // encoded list
.split(/([A-Z]+|[a-z]+)/)       // split it by character case, which gives:
// [ '', 'HE', '', 'el', '', 'LI', '', 'ith', etc. ]
.map((r, i, a) =>               // for each item 'r' at position 'i' in this array 'a':
i % 4 - 3 ?                   //   if we're not currently pointing to an element name:
0                           //     do nothing
:                             //   else:
o[e = a[i - 2]] =           //     save o[e], where e = element symbol
i > 339 ?                 //     if symbol/name first letters do not match:
r[0].toUpperCase() +    //       we need to capitalize the first letter of the name
r.slice(1)              //       and keep the rest unchanged
:                         //     else:
e[0] +                  //       we use the first letter of the symbol,
r +                     //       followed by the name,
(i < 235 ? 'ium' : '')  //       followed by the 'ium' suffix when appropriate
)                               // end of map()


Covering the input string

We try to replace all upper-case letters in the input string with element symbols, using the recursive function g() which eventually returns a replacement string or undefined if no exact cover is found:

g = ([c,                                  // c = next character
...s],                           // s = array of remaining characters
r) =>                     // r = replacement string
c ?                                     // if there's still at least one character:
c < 'A' | c > 'Z' ?                   //   if it's not an upper-case letter:
g(s, r + c)                         //     just append it to 'r'
:                                     //   else:
o[c] && g(s, r + o[c]) ||           //     try to find a symbol matching 'c'
o[c += s.shift()] && g(s, r + o[c]) //     or a symbol matching 'c' + the next char.
:                                       // else:
r                                     //   success: return 'r'


# Javascript, 14871351124611701243 1245 bytes

saved 234 bytes thanks to @musicman523

saved 174 bytes thanks to @ovs

saved 7 bytes thanks to @Shaggy

added 75 bytes to make it work for 2 letter elements

b=>~((o=0,d=Array.from([...b].map((u,i,a)=>(h=e=>("he0Hel2h0Hydrogen1li0Lith2be0Beryll2b0Boron1c0Carbon1n0Nitrogen1o0Oxygen1f0Fluorine1ne0Neon1na0Sod2mg0Magnes2al0Aluminum1p0Phosphorus1s0Sulfur1cl0Chlorine1ar0Argon1k0Potass2ca0Calc2ti0Titan2v0Vanad2cr0Chrom2mn0Manganese1fe0Iron1ni0Nickel1cu0Copper1zn0Zinc1ga0Gall2ge0German2as0Arsenic1se0Selen2br0Bromine1kr0Krypton1rb0Rubid2sr0Stront2y0Yttr2zr0Zircon2nb0Niob2mo0Molybdenum1tc0Technet2ru0Ruthen2rh0Rhod2pd0Pallad2ag0Silver1cd0Cadm2in0Ind2te0Tellur2i0Iodine1xe0Xenon1cs0Ces2ba0Bar2la0Lanthanum1ce0Cer2pr0Praseodym2nd0Neodym2pm0Prometh2sm0Samar2eu0Europ2gd0Gadolin2tb0Terb2dy0Dyspros2ho0Holm2er0Erb2tm0Thul2lu0Lutet2hf0Hafn2ta0Tantalum1w0Tungsten1re0Rhen2ir0Irid2pt0Platinum1au0Gold1hg0Mercury1tl0Thall2at0Astatine1rn0Radon1fr0Franc2ra0Rad2ac0Actin2th0Thor2pa0Protactin2u0Uran2np0Neptun2pu0Pluton2am0Americ2cm0Cur2bk0Berkel2cf0Californ2es0Einstein2fm0Ferm2md0Mendelev2no0Nobel2lr0Lawrenc2rf0Rutherford2db0Dubn2sg0Seaborg2bh0Bohr2hs0Hass2mt0Meitner2ds0Darmstadt2rg0Roentgen2nh0Nihon2fl0Flerov2mc0Moscov2lv0Livermor2ts0Tennessine1og0Oganesson".replace(/2/g,'ium1').split(1).map(a=>a.split(0)).find(a=>a[0]==e)||[,0])[1],o?(o=0,''):((p=a[i+1])&&(o=1,h(u+p))||(o=0,h(u)))))).join).search(0))?b:d


(Slightly more) readable version:

b=>~((o=0,d=Array.from([...b].map((u,i,a)=>(h=e=>(
"he0Hel2h0Hydrogen1li0Lith2be0Beryll2b0Boron1c0Carbon1n0Nitrogen1o0Oxygen1f0Flu
orine1ne0Neon1na0Sod2mg0Magnes2al0Aluminum1p0Phosphorus1s0Sulfur1cl0Chlorine1ar0
cu0Copper1zn0Zinc1ga0Gall2ge0German2as0Arsenic1se0Selen2br0Bromine1kr0Krypton1rb
0Rubid2sr0Stront2y0Yttr2zr0Zircon2nb0Niob2mo0Molybdenum1tc0Technet2ru0Ruthen2rh0
olin2tb0Terb2dy0Dyspros2ho0Holm2er0Erb2tm0Thul2lu0Lutet2hf0Hafn2ta0Tantalum1w0Tu
ngsten1re0Rhen2ir0Irid2pt0Platinum1au0Gold1hg0Mercury1tl0Thall2at0Astatine1rn0Ra
(o=0,''):((p=a[i+1])&&(o=1,h(u+p))||(o=0,h(u)))))).join).search(0))?b:d

• @Gryphon You could also try factoring out on. Also, if you want to make it more human readable, use more readable delineators than 0 and 1. E.g. any of ,;.!/-_:~ *|=+'". – DanTheMan Jul 24 '17 at 1:01