You will write the reader at the heart of a molar mass calculator. It turns a formula string into a dictionary of symbols and counts, then a second function opens one bracket.
The idea. re.findall(r"([A-Z][a-z]?)(\d*)", formula) gives pairs of a symbol and its digits; an empty count means one atom. A bracket with a number multiplies every atom inside it.
Your tasks.
count_simple, return a dictionary of counts for a formula without brackets. Add up repeated symbols.molar_mass, return the molar mass using count_simple and atomic.count_bracket, handle a formula with one bracket, such as Al(OH)3.Have you tried it?
Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.
Log in to join the discussion.
Loading…