28
6
In this challenge, your task is to take an anion and a cation, and output the chemical formula of the compound. The input follows these rules:
- Take in 2 strings (in any order) representing the anion and cation, e.g.
F
,NH_4
, orAl
. - To take in the charge of each ion, you can either have it as part of the string separated by a caret (e.g.
F^-1
) or take in additional numerical arguments.- Note: As long as your numeric input type is signed, then the anion's charge will be passed in as a negative number.
- The symbols will always be real, and charges accurate.
The output should follow these rules:
- Use
_
for subscripts: Fe2O3 would beFe_2O_3
. - Cation first: NaCl, not ClNa.
- Neutral molecule: Li2O, not LiO or LiO-.
- Lowest possible coefficients: Fe2O3, not Fe4O6.
- No subscript ones: NaCl, not Na1Cl1.
- No shifting: NH4OH, not NH5O.
- Conditional parentheses:
- Do not use parentheses on a single-atom ion: MgCl2, not Mg(Cl)2.
- Do not use parentheses if there is only one of the ion per molecule: KClO3, not K(ClO3).
- DO use parentheses if there are two or more of a polyatomic ion: Be3(PO4)2, not Be3PO42 or Be3P2O8.
Here are some additional sample inputs and outputs:
Input Output
Fe^+3, O^-2 Fe_2O_3
Fe^+2, O^-2 FeO
H^+1, SO_4^-2 H_2SO_4
Al^+3, SO_4^-2 Al_2(SO_4)_3
NH_4^+1, SO_4^-2 (NH_4)_2SO_4
Hg_2^+2, PO_4^-3 (Hg_2)_3(PO_4)_2
NH_4^+1, OH^-1 NH_4OH
Hg_2^+2, O_2^-2 Hg_2O_2
Since this is code-golf, the shortest answer in bytes wins.
3Recommended test case:
Fe^+2, OH^-1: Fe(OH)_2
for a polyatomic ion with 1 of each element (OH^-1
). – pizzapants184 – 2017-11-13T06:38:02.703Recommended test case:
C(NH_2)_3^+1, NO_3: C(NH_2)_3NO_3
– Adám – 2017-11-13T11:09:16.3071@Adám second ion is charged:
NO_3^-1
. Also another test case should be the first one paired up with a^-2
, so it would make(C(NH_2)_3)_2...
. Or a case where the ion that is needed more than once begins with a bracket. – Heimdall – 2017-11-13T11:43:34.427@Heimdall The missing
^-1
is a typo. Yes, I wanted such a compound, but didn't find any real-life examples. – Adám – 2017-11-13T11:50:49.4001@Adám
Fe_4(Fe(CN)_6)_3
for Prussian blue. – Colera Su – 2017-11-13T12:32:17.383Recommended test case:
Fe^+3, Fe(CN)_6^-4: Fe_4(Fe(CN)_6)_3
– Adám – 2017-11-14T11:50:47.8803This might be the funniest challenge title I've seen on this website so far, and that's saying a lot for a ppcg question. Thanks for the good laugh – osuka_ – 2017-11-15T13:56:35.383
1@osuka_ Have you seen "what's the fastest way to kill my family" over on Arqade? Or my other PPCG challenge, "confirm the Illuminati"? – Nissa – 2017-11-15T14:06:22.333
@StephenLeppik I meant on ppcg itself, maybe I should’ve used “community” instead of “website” - but ppcg is probably the funniest community on SE, so the meaning is mostly the same. I have seen the Illuminati question though, I got a hearty laugh out of that too – osuka_ – 2017-11-15T14:16:13.873
Can I assume the absolute value of a charge is no more than ... maybe 4 or 6? – Heimdall – 2017-11-16T11:31:16.903
@Heimdall ions do exist with charges of up to +9… but no more than that. Anions go up to -4 (VERY rarely) and cations go up to +9 (one element only). – Nissa – 2017-11-16T13:46:04.790
Thanks. When you say one element only, I assume you mean multi-atom ions have smaller upper limit. So, can I safely assume down to -4 and up to +9? – Heimdall – 2017-11-16T14:25:27.613
@Heimdall yes. Also, what I really meant was "there's only one cation in total with +9". (And the only polyatomic cation I know is ammonium (NH4) which is +1.) – Nissa – 2017-11-16T14:47:08.123