11
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
Where:
C
is the number of carbon atomsN
is the number of nitrogen atomsX
is the number of halogen atoms (F
,Cl
,Br
,I
)H
is the number of hydrogen atoms
Test cases:
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.
Rules
Standard IO rules and loopholes apply. Input must be a standard string, and you can assume the input won't be empty. This is codegolf - so the shortest code in bytes wins.
Proposed test cases: Sodium oxide:
Na2O
and Methylidyne:CH
andCCl4He
. These are some corner cases that may break a few solutions. By the way, not that it matters for anyone other than Mathematica (probably), but can we assume that the compounds (can) exist? – Stewie Griffin – 2017-06-27T21:35:42.253I don't understand
C9H2O1 --> 0
. Shouldn't it be 9?(2*9+2+0-0-2)/2
– DLosc – 2017-06-28T07:34:17.453according to last paragraph, do you mean the code must be able to deal with invalid inputs? By the way, is it guaranteed that every single element in the compound has a trailing '1' as in C1H1? – Keyu Gan – 2017-06-28T08:38:21.603
@KeyuGan yes and yes. – Archie Roques – 2017-06-28T08:40:52.320