Naming ionic compounds

8

1

An ionic compound is named like so: (metal name) (non-metal suffixed with -ide). Here is a list of all the suffixes that need to be replaced with -ide:

-on -ogen -ygen -ine -orus -ur -ic -ium

The number of atoms of each element in the compound is not used in naming ionic compounds. Ignore the multivalent metal naming rules.

NaCl -> sodium + chlorine -> sodium chloride.
K2S -> potassium + sulphur -> potassium sulphide (or sulfide).
Yb2P3 -> ytterbium + phosphorus -> ytterbium phosphide.
PbTe2 -> lead + tellurium -> lead telluride.

A string containing an ionic compound (NaCl, K2S, Yb2P3) will be given as input. No polyatomic ions will be given. The program has to output the name of this compound.

Since nobody wants to compile the periodic table data themselves, here is a paste that has the names, symbols, and whether or not it is a metal of each element, in this order: (name) (symbol) (is metal?). You can save this to an external file (the length of the file will not be counted), or you can store the data within your program (just discount the length of that string from there.) You may not need all the information provided. Every element is seperated by a newline.

As always with code golf challenges, shortest code wins.

beary605

Posted 2012-12-18T02:12:37.687

Reputation: 3 904

1Does "Ignore the naming rules for multivalent metals" mean it is OK to omit the "(II)" in "copper(II) chloride"? – PleaseStand – 2012-12-18T06:27:27.203

2The specification and the examples contradict. Following the spec, the names would be sodium chloride, potassium sulide, ytterbium phosphoride and lead telluriide. – Peter Taylor – 2012-12-18T09:20:15.117

Also, you haven't specified the input format. – Peter Taylor – 2012-12-18T09:21:01.760

@PleaseStand: Yes – beary605 – 2012-12-18T17:07:17.333

@PeterTaylor: :X I will fix that – beary605 – 2012-12-18T17:08:51.093

1These aren't even the correct rules for naming ionic compounds! My chemistry teacher would never approve. – PhiNotPi – 2012-12-19T02:45:55.380

@PhiNotPi: D: I was afraid I had it wrong/poorly explained! What is wrong about the rules (excluding multivalent metal names)? – beary605 – 2012-12-19T03:21:58.027

1Well, your question seems to be limited to ionic compounds that contain two atoms, but you didn't specify that. Ionic compounds are made of two unique types of ion, in a ratio that makes the compound neutral. These ions can have one atom each, or they can be (and often are) polyatomic ions. If you mix the ammonium ion (NH4 +) with the sulfate ion (SO4 2-), you get ammonium sulfate ((NH4)2SO4).

I guess my main point is that you are forgetting about an entire class of ionic compounds, even when excluding multivalent metal names. – PhiNotPi – 2012-12-19T03:48:48.867

@PhiNotPi: Ah, that's what else I forgot to exclude :P Sorry about that. – beary605 – 2012-12-19T05:24:01.523

Answers

1

Ruby, 114

f,s=$*[0].split /\d*(?=[A-Z]|\Z)/
puts (n=Hash[*<<X.split-%w[0 1]].invert)[f]+" "+n[s].sub(/(i..|.gen|..)\Z/,"ide")
Hydrogen H 0
Helium He 0
Lithium Li 1
Beryllium Be 1
Boron B 0
Carbon C 0
Nitrogen N 0
Oxygen O 0
Fluorine F 0
Neon Ne 0
Sodium Na 1
Magnesium Mg 1
Aluminium Al 1
Silicon Si 0
Phosphorus P 0
Sulfur S 0
Chlorine Cl 0
Argon Ar 0
Potassium K 1
Calcium Ca 1
Scandium Sc 1
Titanium Ti 1
Vanadium V 1
Chromium Cr 1
Manganese Mn 1
Iron Fe 1
Cobalt Co 1
Nickel Ni 1
Copper Cu 1
Zinc Zn 1
Gallium Ga 1
Germanium Ge 1
Arsenic As 0
Selenium Se 0
Bromine Br 0
Krypton Kr 0
Rubidium Rb 1
X

Uses a paste of the element text that I haven't counted in the score. I remove the metal tag (since I'm assuming the compounds are always given with the metal first, so I don't actually need it) and build a dictionary with it. I identify the part to replace with "ide" by checking the end of the string for three letters starting with i, four letters ending with gen, or otherwise just the last two characters.

histocrat

Posted 2012-12-18T02:12:37.687

Reputation: 20 600

What does it make of chlorine and phosphorus for example? – Graham – 2012-12-19T17:29:24.693

NaCl => Sodium Chloride (meets spec). Yb2P3 => Ytterbium Phosphoride (doesn't meet current spec). It also renders Nitrogen as Nitrogide rather than Nitride. Now that the "syllable" part of the spec is gone, I'll probably have to steal your hardcoded list. – histocrat – 2012-12-19T20:33:54.443

Okay, should meet the spec again. – histocrat – 2012-12-21T01:17:29.190

1

Python, 134 Characters.

#T is a string containing the provided information on the elements
from re import*
print sub('(orus|[oy]ge|i?..)$','ide',' '.join(split(r'(\w+) %s '%e,T)[1]for e in findall('[A-Z][a-z]?',raw_input())))

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

Nolen Royalty

Posted 2012-12-18T02:12:37.687

Reputation: 330

0

Mathematica 252 267 254

Code

ElementData[] contains information about the elements; it is native to Mathematica.

d=ElementData; a=Reverse@SortBy[d[#,"Abbreviation"]&/@d[],StringLength[#]&];
g[e_]:=Row[{e," \[RightArrow] ", p=Row[z=(d[#,"StandardName"] &/@StringCases[e,a]),
  " + " ] , " \[RightArrow] ",p[[1,1]]<> " "<>StringReplace[p[[1,2]],
  f__~~z:"on"|"ogen"|"ygen"|"ine"|"orus"|"ur"|"ic"|"ium":> f~~"ide"] }]

Examples

g["NaCl"]
g["K2S"]
g["Yb2P3"]
g["PbTe2"]

compounds


Explanation

The code removes digits from the compound, then separates the elements by their abbreviated names. It then converts the abbreviations to the standard names of elements. Finally, it replaces the non-metal by the term with the correct suffix.

DavidC

Posted 2012-12-18T02:12:37.687

Reputation: 24 524

You don't need the multivalent metals in the output for PbTe2, lead telluride will suffice. – beary605 – 2012-12-19T05:25:34.613

0

APL 142 150

If I am allowed to store the element names and symbols in a pair of nested arrays within the APL workspace then the following code should do it:

(e[s⍳c[1]]),((∊¯1×((+/¨⍳¨i)=+/¨(¯1×i)↑¨t⍳¨⊂n)/i←⍴¨t←'ic' 'on' 'ur' 'ine' 'ium' 'ogen' 'ygen' 'orus')↓n←∊e[s⍳(c←(98>⎕av⍳c)⎕penclose c←⍞~⍕⍳9)[2]]),'ide'

It works roughly as follows:

c←(98>⎕av⍳c)⎕penclose c←⍞~⍕⍳9

Takes the input from the screen via ⍞ and splits the chemical name into symbols and discards any numbers

e[s⍳c[1]]

Looks up the first element name from its symbol. Similarly for c[2]

(∊¯1×((+/¨⍳¨i)=+/¨(¯1×i)↑¨t⍳¨⊂n)/i←⍴¨t←'ic' 'on' 'ur' 'ine' 'ium' 'ogen' 'ygen' 'orus')↓n

Identifies which of the eight possible endings the non-metal has and drops it and then replaces it with ide via:

,'ide'

There is no error trapping so only works for valid compounds dictated via the one or zero identifier in the list of elements provided.

Graham

Posted 2012-12-18T02:12:37.687

Reputation: 3 184

does oxygen -> oxide or hydrogen -> hydride work? – beary605 – 2012-12-20T00:52:39.497

@beary605. They didn't but they do now - thanks. – Graham – 2012-12-20T09:36:00.020