Secret Message Part 1, Elements

8

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.

Your Task:

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
V   Vanadium
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
Pd  Palladium
Ag  Silver
Cd  Cadmium
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
Gd  Gadolinium
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
Pb  Lead
Bi  Bismuth
Po  Polonium
At  Astatine
Rn  Radon
Fr  Francium
Ra  Radium
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
Ds  Darmstadtium
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!

Gryphon

Posted 2017-07-23T19:29:02.687

Reputation: 6 697

Comments are not for extended discussion; this conversation has been moved to chat.

– Dennis – 2017-07-28T16:17:44.150

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 – 2017-07-29T00:00:16.290

@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 – 2017-07-29T11:10:27.187

Answers

2

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]

Jonathan Frech

Posted 2017-07-23T19:29:02.687

Reputation: 6 681

6

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 =

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

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'

Arnauld

Posted 2017-07-23T19:29:02.687

Reputation: 111 334

2

Javascript, 1487 1351 1246 1170 1243 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
Argon1k0Potass2ca0Calc2ti0Titan2v0Vanad2cr0Chrom2mn0Manganese1fe0Iron1ni0Nickel1
cu0Copper1zn0Zinc1ga0Gall2ge0German2as0Arsenic1se0Selen2br0Bromine1kr0Krypton1rb
0Rubid2sr0Stront2y0Yttr2zr0Zircon2nb0Niob2mo0Molybdenum1tc0Technet2ru0Ruthen2rh0
Rhod2pd0Pallad2ag0Silver1cd0Cadm2in0Ind2te0Tellur2i0Iodine1xe0Xenon1cs0Ces2ba0Ba
r2la0Lanthanum1ce0Cer2pr0Praseodym2nd0Neodym2pm0Prometh2sm0Samar2eu0Europ2gd0Gad
olin2tb0Terb2dy0Dyspros2ho0Holm2er0Erb2tm0Thul2lu0Lutet2hf0Hafn2ta0Tantalum1w0Tu
ngsten1re0Rhen2ir0Irid2pt0Platinum1au0Gold1hg0Mercury1tl0Thall2at0Astatine1rn0Ra
don1fr0Franc2ra0Rad2ac0Actin2th0Thor2pa0Protactin2u0Uran2np0Neptun2pu0Pluton2am0
Americ2cm0Cur2bk0Berkel2cf0Californ2es0Einstein2fm0Ferm2md0Mendelev2no0Nobel2lr0
Lawrenc2rf0Rutherford2db0Dubn2sg0Seaborg2bh0Bohr2hs0Hass2mt0Meitner2ds0Darmstadt
2rg0Roentgen2nh0Nihon2fl0Flerov2mc0Moscov2lv0Livermor2ts0Tennessine1og0Oganesson
".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

SuperStormer

Posted 2017-07-23T19:29:02.687

Reputation: 927

"readable version", yeah, totally readable. Nice job though. This is going to be a byte-intensive challenge for any lang that doesn't have a built-in for elements (anything that isn't Mathematica). – Gryphon – 2017-07-23T20:41:38.987

1Can you factor out "ium" to save bytes? – musicman523 – 2017-07-23T20:42:37.553

That's a good idea, @musicman523 – Gryphon – 2017-07-23T20:42:56.037

@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 – 2017-07-24T01:01:45.207

@DanTheMan no, that's 5 bytes longer – SuperStormer – 2017-07-24T01:21:22.667

You don't need to encode elemnts like copernicium (CN) as you can always use carbon and nitrogen instead – ovs – 2017-07-24T04:58:54.887

Expanding on @ovs' improvments: 1170 bytes

– Shaggy – 2017-07-24T14:54:55.103

I only saved you 7 bytes; credit for the other 69 should go to @ovs. – Shaggy – 2017-07-24T22:28:26.837

@Shaggy fixed that – SuperStormer – 2017-07-25T00:26:08.170

1This fails to identify 2-character elements in the input string. – Arnauld – 2017-07-25T08:33:39.887