Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a Chemistry Formula in Python

I am trying to solve this problem: https://leetcode.com/articles/number-of-atoms/#approach-1-recursion-accepted.

The question is: given a formula like C(Mg2(OH)4)2, return a hash table with elements and their counts. Element names always start with a capital letter and may be followed by a small letter.

I thought that I will first start by solving the simplest case: no brackets.

def bracket_hash(formula):
    element = ""
    atom_count = 0
    element_hash = {}

    for x in formula:
        if x.isupper():
            if element!="":
                element_hash[element] = 1
                element = ""
            element = x

        elif x.islower():
            element += x            

        else: 
            element_count = int(x)
            element_hash[element] = element_count
            element_count = 0
            element = ""

    if element!="":
        element_hash[element] = 1

    return element_hash

This code works perfectly fine for cases like:

print(bracket_hash("H2O"))
print(bracket_hash("CO2"))
print(bracket_hash("Mg2O4"))
print(bracket_hash("OH"))

Now I thought that somehow stacks must be used to handle the case of multiple brackets like OH(Ag3(OH)2)4, here Ag's count has to be 3*4 and O and H's count will be 2*4 + 1.

So far I started with something like this:

def formula_hash(formula):
    stack = []
    final_hash = {}
    cur = ""
    i = 0

    while i < len(formula):
        if formula[i] == '(':
            j = i
            while formula[j]!=')':
                j = j + 1
            cur = formula[i:j]
            stack.append(bracket_hash(cur))
            cur = ""
            i = j + 1

but now I am stuck.

I kind of get stuck as coding problems get longer and involved a mix of data structures to solve. Here they use Hash table and stack.

So my question is: how to break down this problem into manageable parts and solve it. If I am really solving this problem I have to map it to manageable code segments. Any help would be greatly appreciated.

Thanks.

like image 279
mourinho Avatar asked May 20 '26 08:05

mourinho


1 Answers

I think you can use recursivity to solve this problem. Here is how your function should work:

  • Do like you do in the first code, until you encounter an opening parenthesis.
  • When you encounter an opening parenthesis, find the corresponding closing parenthesis. This can be done with a counter: initialize it to 1, then when you encounter a new opening parenthesis, you increment the counter, and when you encounter a closing parenthesis you decrement it. When the counter equals 0, you have found the matching closing parenthesis.
  • Cut the string between parentheses and call the same function with this string (here's the recursive aspect).
  • Add the values in the returned dictionary to the current dictionary, multiplied by the number which follows the parenthesis.

If you have problems implementing some parts of this solution, tell me and I will give more details.

EDIT: about the stack approach The stack approach just simulates recursivity. Instead of calling the function again and having local counter, it has a stack of counters. When an opening parenthesis is opened, it counts in this context, and when it's closed it merges it with the context which contains it, with corresponding multiplicity.

I prefer by far the recursive approach, which is more natural.

like image 138
nyr1o Avatar answered May 21 '26 21:05

nyr1o



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!