Task
Write a program that takes in a compound made solely of elements with an atomic number less than or equal to 92 (Uranium), and outputs the molar mass of the compound in grams/mole
.
Rules and Restrictions
- You may not use a function that directly calculates molar masses for you.
- Your program must be able to run on an offline machine.
- You MAY use a separate data file. Contents of this file must be provided.
- Your score is the length of your program in bytes, plus the the length of your data file in bytes, should you choose to use one.
- This is code-golf, therefore, lowest score wins.
Input
A string containing the compound. This string may be read through STDIN
, passed as an argument, or set to a variable (please specify which). The following are all valid inputs:
CH2
(one Carbon, two Hydrogens)(CH2)8
(8 Carbons, 16 Hydrogens)U
(1 Uranium)
Your program is not required to function for embedded parentheses (i.e. ((CH3)2N4)4
), nor for any compound that contains an element with atomic number greater than 92. If your program does not function for either of the above two cases, it must output Invalid formula
for such cases - no undefined behavior.
Output
You should output, to STDOUT
or to a variable, the molar mass of the compound in grams/mole
. A list of atomic masses of elements is available here (Wikipedia). Your answer should be accurate to the decimal place for compounds containing fewer than 100 atoms (rounded).
If the input is invalid, Invalid formula
must be outputted.
Information on calculating the molar mass of a compound in grams/mole
(Wikipedia).
Examples
Input Output
H2O 18.015
O 15.999 (16 acceptable)
C6H2(NO2)3CH3 227.132
FOOF 69.995
C6H12Op3 Invalid formula
Np 237 (or Invalid formula)
((C)3)4 144.132 (or Invalid formula)
CodeGolf Invalid formula
U1000000
is harder to determine to the decimal point than the mass ofU2
\$\endgroup\$13
if the atomic mass is12.999
? \$\endgroup\$