Degree of Unsaturation
This is not a particularly hard code puzzle - but I'm interested to see your multiple ways of solving it.
The Degree of Unsaturation is the number of double chemical bonds between atoms, and/or the number rings in a chemical compound.
You will be given the molecular formula of a chemical compound in the form XaYbZc (where a, b and c are the number of atoms of X, Y or Z in the compound) - the formula could be of any length and contain any chemical element in the periodic table (though elements other than C, H, N, F, Cl, Br, I may be ignored as they do not feature in the formula). The compound will contain at least one atom of carbon. You must calculate and display its Degree of Unsaturation.
For example, the compound benzene (pictured below) has a DoU of 4 as it has three double bonds (shown by a double line between atoms), and a single ring (a number of atoms connected in a loop):
As defined by LibreTexts:
DoU = (2C + 2 + N − X − H ) / 2
Cis the number of carbon atoms
Nis the number of nitrogen atoms
Xis the number of halogen atoms (
His the number of hydrogen atoms
C6H6 --> 4 C9H2O1 --> 0 C9H9N1O4 --> 6 U1Pt1 --> Not a valid input, no carbon Na2O1 --> Not a valid input, no carbon C1H1 --> 1.5, although in practice this would be one, but is a part of a compound rather than a compound in entirety. N1H3 would return 0 - though in practice it isn't an organic compound (in other words it contains no carbon) so the formula wouldn't apply and it isn't a valid input
For an explanation of CH see here
In essence, you must identify if there are any of the above elements (C, H, N, F, Cl, Br, I) in the compound, and if so how many there are. Then, calculate the Degree of Unsaturation using the above formula.
Only C, H, N, F, Cl, Br, and I are valid inputs for the DoU formula. For the purposes of this puzzle, any other elements may be completely ignored (eg if the compound were C6H6Mn the result would still be 4). If there are none of the above compounds the answer would be zero.
You may assume that all the compounds input are chemically possible, contain at least one atom of carbon, and are known to exist. If the input is invalid, the program may output either 0 or -1, or produce no result.