# Python doubt (loop, split and more ….)

## Question:

Let's see if you can help me with this community issue.

Given a molecule represented by a string like `C9-H8-O4` calculate its atomic mass. For example, for a `C4-H3` molecule, the atomic mass would be `4 * 12.01 + 3 * 1.007825` .

I have to do a general solution by accessing the dictionary using the key, which in this case will be the type of atom. For example, for the `C5-H3` molecule you should do something like this:

• Separate the molecule by the dashes ( with the `split` function? ).
• For each of the parts, `C5` and `H3` , find the type of atom: `C` and `H` ( Any loop? )
• Access the dictionary of masses and, for the key that corresponds to the type of atom, obtain the mass.
• Once the mass is found, multiply it by the number of atoms found.

I have this written:

# Atomic masses

``````masas = {'H': 1.007825, 'C': 12.01, 'O': 15.9994, 'N': 14.0067, 'S': 31.972071,
'P': 30.973762}

def calcula_masa_atomica(molecula):
"""
Calcula la masa atomica de una molecula
"""
masa = 0.0
return masa
``````

# Code to complete

``````print calcula_masa_atomica('C13-H18-O2')
print calcula_masa_atomica('C8-H10-N4-O2')
print calcula_masa_atomica('C20-H25-N3-O')
print calcula_masa_atomica('C20-H10-O2-P2-S')
``````

I think the best option is to use regular expressions . I give you an example code:

``````import re

# Grupos de varias letras, seguidas (o no) de números
pat = re.compile("([a-zA-Z]+)(\d*)")
masas = {'H': 1.007825, 'C': 12.01, 'O': 15.9994, 'N': 14.0067, 'S': 31.972071,
'P': 30.973762}

def calcula_masa_atomica(molecula):
grupos = [(atom, int(n) if n else 1)
for (atom, n) in pat.findall(molecula)]

masa = sum(n*masas.get(atom,0.0) for (atom,n) in grupos)
return masa
``````

If there were any symbols in the formula for unknown mass, `0.0` is taken as the atomic weight. It may be better to give an exception.

Scroll to Top