Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
    
Does "Ignore the naming rules for multivalent metals" mean it is OK to omit the "(II)" in "copper(II) chloride"? –  PleaseStand Dec 18 '12 at 6:27
2  
The specification and the examples contradict. Following the spec, the names would be sodium chloride, potassium sulide, ytterbium phosphoride and lead telluriide. –  Peter Taylor Dec 18 '12 at 9:20
    
Also, you haven't specified the input format. –  Peter Taylor Dec 18 '12 at 9:21
1  
These aren't even the correct rules for naming ionic compounds! My chemistry teacher would never approve. –  PhiNotPi Dec 19 '12 at 2:45
1  
Well, 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 Dec 19 '12 at 3:48

4 Answers 4

up vote 0 down vote accepted

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.

share|improve this answer
    
What does it make of chlorine and phosphorus for example? –  Graham Dec 19 '12 at 17:29
    
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 Dec 19 '12 at 20:33
    
Okay, should meet the spec again. –  histocrat Dec 21 '12 at 1:17

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"""
share|improve this answer

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.

share|improve this answer
    
does oxygen -> oxide or hydrogen -> hydride work? –  beary605 Dec 20 '12 at 0:52
    
@beary605. They didn't but they do now - thanks. –  Graham Dec 20 '12 at 9:36

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.

share|improve this answer
    
You don't need the multivalent metals in the output for PbTe2, lead telluride will suffice. –  beary605 Dec 19 '12 at 5:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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