Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
eval(pythonize(formula)) solution in Clear category for Chemical Analysis by flpo
import re
from collections import Counter
rules = (
('([A-Z][a-z]?)', r'"\1"'),
('([0-9])', r'*\1'),
('([0-9]|")("|\()', r'\1+\2')
)
def pythonize(formula):
formula = formula.translate(str.maketrans('[]', '()'))
for pattern, repl in rules:
formula = re.sub(pattern, repl, formula)
return formula
def atoms(formula, limit):
count = Counter(re.findall('[A-Z][a-z]?', eval(pythonize(formula))))
return [e for e, c in sorted(count.items()) if c >= limit]
Sept. 1, 2018
Comments: