Neutralization reaction
Description:
Concept
In this kata we will have the opportunity to remember one of the most fundamental chemical reactions — neutralization. In the context of a chemical reaction the term neutralization is used to describe the reaction between an acid and a base (or alkali). This is an overly simplified approach that will help us just put a few things into perspective while keeping this kata at an entertaining level without the need to peruse dusty tomes of chemistry to make things work. A very simple example of neutralization is that of hydrochloric acid and sodium hydroxixe which react to yield sodium chloride and water:
HCl + NaOH → NaCl + H2O
This is the form of the reaction that you are asked to compose, as a string, given two different molecular formulae (also strings). The arrow sign (→) in the above reaction means that it is complete, that is neutralization is a quantitative reaction. Although this in principle is true in practice the reaction is as complete as the electrolytic dissociation of its respective reagents allows, before reaching a state of chemical equilibrium between the undissolved substances and their constituent ions. For the sake of simplicity all bases and acids are considered strong (fully dissociate in aqueous solutions) thus in this kata we deal only with the above simplified complete reaction and not other intermediate reactions with molecules of water or compounds that dissolve gradually yielding different types of ions. For a more detailed and realistic approach of how things really work feel free to explore the links provided throughout this description, but more closely the Ostwald–Arrhenius electrolytic dissociation theory1 and Brønsted–Lowry's acid–base theory2 at the end.
Acids are the proton (H+) givers and bases the proton recipients, but for our purposes we will sniff them out by just inspecting their molecular formulae: every compound whose formula starts with hydrogen (H) must be an acid and each that ends with a hydroxide group (OH) a base. The valence of a hydrogen (potential charge when in ionic form) is +1 and that of the hydroxide -1. Also note that hydroxides will appear within parentheses when they appear more than one time in the base formula. When the base and the acid react they will exchange their respective ions (which are produced during dissociation in aqueous solutions) yielding molecules of water and the salt product of combining the cation3 of the base with the anion of the acid. Remember that conventionally the formula starts with the positively charged part of the compound and ends with the negatively charged part.
The compounds you are going to be given as arguments are solely inorganic, so do not expect long-winded and overly complicated organic formulae. Moreoever, many anions in acids tend to be composed of many different atoms (e.g. PO4-3 from H3PO4 ) whereas bases usually include a single cation (e.g. K+ from KOH), with the exception of ammonium hydroxide — NH4OH.
Requirements
Write a function named neutralize which takes two string arguments. The arguments are guaranteed to represent valid molecular formulae of the reactants.
The function must return a formatted string representing the quantitative neutralization reaction of the two reactants. The string should respect the following requirements:
- The symbol that will be used, by convention, for printing the right arrow of the reaction is unicode character \u2192.
- All compounds and the arrow will be delimited by 1 space from each other, apart from the compound coefficients (if any, see final note on stoicheiometry) which will be printed immediately before the compound without spaces.
- Ions occurring more than once in the formula (multiplicity > 1) have to be enclosed in parentheses only if they are non-monoatomic (composed of many different atoms instead of just one). Said multiplicity is noted right after the closing parenthesis, e.g. "Mg(OH)2", "Ca(OH)2". When the multiplicity is 1 (single occurrence for that ion) or the ion is monoatomic (a single element forms the ion) parentheses should not appear, e.g. "HCl", "NaOH", "KOH", "H3PO4".
- The reactants can be given in any order and combination (acid-acid, base-base, acid-base, base-acid). You must print the reactants in encounter order (argument order), but when printing the products always print the salt first and the water last (see first 2 examples).
- Mind the stoichiometry4 of the reaction by balancing the coefficients of reactants and products so that the sum of all atoms for each element on the left side equals that on the right side of the reaction equation. Take for example the reaction: 2HCl + Mg(OH)2 → MgCl2 + 2H2O. All atoms are balanced as follows: 2 Cl- anions on the left balance out 2 Cl- on the right. Similarly 1 Mg+2, 4 H+ and 2 O-2 appear in either side of the reaction equation. Notice that coefficients of 1 are not written as they are implied by the mere presence of the molecular formula, but greater numbers are printed just before the associated compound.
- The coefficients in the reaction and the multiplicities of atoms in the formula should be the coprime numbers (their greatest common divisor is 1).
- We assume that same type of reagents will not react with each other. For example, we do not expect a reaction between NaOH and KOH, or HCl and HNO3 to occur.
Examples
The following are examples of expected inputs and outputs to set you on the right track. [Remember that most of what is written is just an overly simplified model of the concepts involved and not scientifically accurate in a strict sense.]
```javascript // same reactants in different argument order yield the exact same products neutralize('HCl', 'NaOH'); // 'HCl + NaOH → NaCl + H2O' neutralize('NaOH', 'HCl'); // 'NaOH + HCl → NaCl + H2O'// not possible reactions neutralize('NaOH', 'KOH'); // null, because base cannot react with base neutralize('HBrO3', 'HIO4'); // null, because acid cannot react with acid
// reactants with valences that are coprimes or both 1 neutralize('HCl', 'NaOH'); // 'HCl + NaOH → NaCl + H2O' neutralize('HCl', 'Hg(OH)2'); // '2HCl + Hg(OH)2 → HgCl2 + 2H2O' neutralize('HNO4', 'Co(OH)2'); // '2HNO4 + Co(OH)2 → Co(NO4)2 + 2H2O' neutralize('HClO3', 'Cr(OH)5'); // '5HClO3 + Cr(OH)5 → Cr(ClO3)5 + 5H2O' neutralize('H2SO4', 'Ti(OH)3'); // '3H2SO4 + 2Ti(OH)3 → Ti2(SO4)3 + 6H2O'
// reactants with valences that are not coprimes neutralize('H2S2O4', 'Ba(OH)2'); // 'H2S2O4 + Ba(OH)2 → BaS2O4 + 2H2O' neutralize('H2CO2', 'Mn(OH)4'); // '2H2CO2 + Mn(OH)4 → Mn(CO2)2 + 4H2O' neutralize('H3As', 'Fe(OH)3'); // 'H3As + Fe(OH)3 → FeAs + 3H2O'
```coffeescript
# same reactants in different argument order yield the exact same products
neutralize 'HCl', 'NaOH' # 'HCl + NaOH → NaCl + H2O'
neutralize 'NaOH', 'HCl' # 'NaOH + HCl → NaCl + H2O'
# not possible reactions
neutralize 'NaOH', 'KOH' # null, because base cannot react with base
neutralize 'HBrO3', 'HIO4' # null, because acid cannot react with acid
# reactants with valences that are coprimes or both 1
neutralize 'HCl', 'NaOH' # 'HCl + NaOH → NaCl + H2O'
neutralize 'HCl', 'Hg(OH)2' # '2HCl + Hg(OH)2 → HgCl2 + 2H2O'
neutralize 'HNO4', 'Co(OH)2' # '2HNO4 + Co(OH)2 → Co(NO4)2 + 2H2O'
neutralize 'HClO3', 'Cr(OH)5' # '5HClO3 + Cr(OH)5 → Cr(ClO3)5 + 5H2O'
neutralize 'H2SO4', 'Ti(OH)3' # '3H2SO4 + 2Ti(OH)3 → Ti2(SO4)3 + 6H2O'
# reactants with valences that are not coprimes
neutralize 'H2S2O4', 'Ba(OH)2' # 'H2S2O4 + Ba(OH)2 → BaS2O4 + 2H2O'
neutralize 'H2CO2', 'Mn(OH)4' # '2H2CO2 + Mn(OH)4 → Mn(CO2)2 + 4H2O'
neutralize 'H3As', 'Fe(OH)3' # 'H3As + Fe(OH)3 → FeAs + 3H2O'
Learn more
- The Ostwald and Arrhenius' electrolytic dissociation theory, about certain compounds called electrolytes which dissociate in solutions to give ions.
- A more general definition of the neutralization reaction based on Brønsted–Lowry acid–base theory.
- Anion is an alias for the negatively charged ion and cation for the positive one.
- The origin of the word stoichiometry is greek (stoicheion "element" and μέτρον metron "measure") and means the calculation of relative quantities of reactants and products in chemical reactions. Read more here.
- Wiki link on neutralization here
Similar Kata:
Stats:
Created | Apr 9, 2017 |
Published | Mar 18, 2018 |
Warriors Trained | 377 |
Total Skips | 25 |
Total Code Submissions | 543 |
Total Times Completed | 39 |
JavaScript Completions | 23 |
CoffeeScript Completions | 1 |
Java Completions | 17 |
Total Stars | 15 |
% of votes with a positive feedback rating | 92% of 13 |
Total "Very Satisfied" Votes | 12 |
Total "Somewhat Satisfied" Votes | 0 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 3 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 6 kyu |