The atomic composition of a molecule is represented by a molecular formula. Such a molecular formula represents the number of atoms of each element that occurs in the molecule. The elements have symbols that consist of a single capital, optionally followed by one or two lower case letters. The proper use of upper and lower case is important: Pb gives the element lead while PB represents a compound of phosphorus and boron. It is common for the elements C, H, N, O and S to be listed first, followed by the other elements in alphabetical order. This is demonstrated in the molecular formula of mustard gas (C$$_\text{4}$$H$$_\text{8}$$SCl$$_\text{2}$$). The following table contains some examples of molecular formulas.

name molecular formula
water H$$_\text{2}$$O
carbon dioxide CO$$_\text{2}$$
ethanol C$$_\text{2}$$H$$_\text{5}$$OH
butyric acid C$$_\text{4}$$H$$_\text{8}$$O$$_\text{2}$$
guanosine triphosphate C$$_\text{10}$$H$$_\text{16}$$N$$_\text{5}$$O$$_\text{14}$$P$$_\text{3}$$

The molecular mass of a substance is the mass of a single molecule of the substance, expressed in atomic mass units (u). This molecular weight can be easily calculated as the sum of the atomic masses of the individual atoms of which the molecule is built up. For example, the molecular weight of water (H$$_\text{2}$$O) is the sum of the masses of the atoms of which a water molecule consists. The periodic table of elements shows that the atomic mass of hydrogen and oxygen, respectively, is equal to $$1,00794\,\text{u}$$ en $$15,9994\,\text{u}$$. The molecular mass of water is$$2 \times 1,00794\,\text{u} + 15,9994\,\text{u} = 18,01528\,\text{u}$$.

Assignment

Given is a text file periodic_system.txt1, which contains a list with information about the elements in the periodic table. Each line - except for the first one which is used as a header - includes the following information about an element: i) atomic number, ii) symbolic representation, iii) English name, iv) Dutch name, and v) atomic mass. The information fields are separated by a tab. Asked:

  1. Write a function atomicmass that takes the location of the text file periodic_system.txt2 as its argument. The function must return a dictionary as a result, which uses the symbolic representation of the elements as keys, and the atomic mass as the corresponding value.

  2. Write a function molecularmass, which returns the molecular mass for a given molecular formula. In addition to the molecular formula itself, a second argument must be passed to the function: a dictionary that uses the symbolic representation of the elements of the periodic table as a key and links to their atomic mass. For simplicity, the various elements in a molecular formula are separated by a hyphen (-). For example, the formula for sulfuric acid is (H$$_\text{2}$$SO$$_\text{4}$$) displayed as the string H2-S-O4. The molecular mass of sulfuric acid can then be calculated as \[\begin{eqnarray*} \text{molecularmass(}\text{H}_\text{2}\text{SO}_\text{4}\text{)}&=& 2\times\text{atomicmass(}\text{H}\text{)} + \text{atomicmass(}\text{S}\text{)} + 4\times\text{atomicmass(}\text{O}\text{)}\\ &=&2\times1,00794\,\text{u}+32,066\,\text{u}+4\times15,9994\,\text{u}\\ &=&98,07948\,\text{u} \end{eqnarray*}\] The function should return the real value -1 if the given molecular formula contains the symbolic representation of an element which is not found in the given dictionary.

Example

In the following interactive session we assume that the text file periodic_system.txt3 is located in the current directory.

>>> index = atomicmass('periodic_system.txt')
>>> index['Si']
28.0855
>>> molecularmass('H2-S-O4', index)
98.07948