Welcome to Project Lovelace! We're still super new so there are still tons of bugs to find and improvements to make. If you have any suggestions, complaints, or any comments at all please let us know on Discourse!

Molecular mass calculator

Molecules are made up of atoms, so to calculate the mass of a molecule you just add up the masses of the atoms but each atom has a different mass.Atoms are so light that using grams to describe their mass is pretty annoying so we use other units like the atomic mass unit (amu). 1 amu is defined as one twelevth the mass of a carbon-12 atom, or $1.661 \times 10^{-27} \; \mathrm{kg}$. For example, water (H2O) is a molecule made up of two hydrogens and an oxygen. The hydrogen (H) atoms have a mass of 1.008 amu while carbon (C) has a mass of 12.011 amu and the heaviest atom on the periodic table, Oganesson (Og), has a mass of 294 amu while a methane molecule (CH4) has a mass of 16.04 amu.

Given the chemical formula of a molecule, return its mass in amu. You might find periodic_table.csv useful. It's a comma-separated file containing all the elements from the periodic table and their masses in amu. The starter code in the code editor below shows how you can read the data from the file and store it in a dictionary in Python.

Input: The molecule's chemical formula.

Output: The molecule's mass in atomic mass units (amu).

Example 1

Input: Pa Output: 231.04

Example 2

Input: OCS Output: 60.08

Example 3

Input: C4H4AsH Output: 128.00

Example 4

Input: C20H25N3O Output: 323.44

 Difficulty  Timesink 60 s 250 MiB molecular_mass(chemical_formula)

Write a function that accepts the input as function parameters and returns the correct output. Make sure to read the description above to produce the correct output in the correct format and use the correct function signature so we can run your code. A good first step is to try reproducing the example(s). Your code must not take longer than the maximum runtime to run and must not use more memory than the allowed limit.

You must be logged in to view your submissions.

• Would be good to include the regex in post-problem notes.

Let us know what you think about this problem! Was it too hard? Difficult to understand? Also feel free to discuss the problem, ask questions, and post cool stuff on Discourse. You should be able see a discussion thread below. Feel free to post your solutions but if you do please organize and document your code well so others can learn from it.