14
1
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
1"to the decimal point" - for how large molecules? The mass of
U1000000
is harder to determine to the decimal point than the mass ofU2
– John Dvorak – 2014-08-02T11:09:23.597to the decimal point for molecules with less than 100 atoms. Added this to the question statement. – es1024 – 2014-08-02T11:16:36.767
I also assume I'm allowed to output
13
if the atomic mass is12.999
? – John Dvorak – 2014-08-02T11:16:55.0901That doesn't sound right @es1024. 13, ok, but 12? – RubberDuck – 2014-08-02T12:16:43.517
1"Your answer should be accurate to the decimal place" does that mean to one decimal place or to the nearest integer? – None – 2014-08-02T13:04:03.937
Sorry for the confusion. If the molar mass is
12.753
,13
is acceptable. To the decimal place means nearest integer is sufficient. – es1024 – 2014-08-02T22:46:13.273What should the output be if it is impossible to obtain the required accuracy? There are quite a few elements with atomic number less than 92 which don't have any stable isotopes and hence don't have a "proper" value in the table you linked. So the error in the molar mass of e.g. Fr3N is more than one atomic unit. – Peter Taylor – 2014-08-08T12:07:12.600
@PeterTaylor use the mass number of the longest lived isotope (as given on the table).
Fr3N
would be223*3+14.007=683.007
, so683
would be a valid answer. – es1024 – 2014-08-08T20:25:04.500I wrote a program that did the opposite, put it on my PSP, and was allowed to use it on an exam. You give it a mass and it gives you the formula. And I would use the weighted average of atomic masses provided on the periodic table, that would be a better representation of natural molecules. – user137 – 2014-08-20T16:52:23.553