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.
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
andlead telluriide
. – Peter Taylor – 2012-12-18T09:20:15.117Also, 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