Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
8-liner: almost easy regex solution in Clear category for Chemical Analysis by przemyslaw.daniel
def atoms(formula, number):
import re
pattern = '([A-Z][a-z]*|\([^\d]+?\)|\[[^\d]+?\])(\d)'
while any(x.isdigit() for x in formula):
block, count = re.search(pattern, formula).groups()
formula = formula.replace(block+count, block*int(count))
result = re.findall('[A-Z][a-z]*', formula)
return sorted(x for x in set(result) if result.count(x) >= number)
Dec. 19, 2018