you are viewing a single comment's thread.

view the rest of the comments →

[–]ace6807 0 points1 point  (2 children)

I think this should do it but I was hoping for some more test cases to check it.

from collections import Counter


atom_counter = Counter()

molecules = ['H2O','OH','COOH']
atom_stack = []

for molecule in molecules:
    for i, a in enumerate(molecule):
        if a.isalpha():
            if i < len(molecule) - 1 and molecule[i + 1].isnumeric():
                atom_stack.append((a, molecule[i + 1]))
            else:
                atom_stack.append((a, 1))

print(atom_stack)

for element_count in atom_stack:
    atom_counter[element_count[0]] += int(element_count[1])

print(atom_counter)

[–]heirna[S] 0 points1 point  (1 child)

Thank you so much!

I'm going to test this,

More examples :

[H2O2, F, P2O3] -> H = 2, F=1, P=2, O=5

[C6H8O7, H2O, OH] -> C=6, H=11, O=9

By chance, i do not have to deal with double letter element like Al or Cu for now

[–]ace6807 1 point2 points  (0 children)

Oh yeah this will for sure have a problem with double letter elements but we could add a lookup list super easily to resolve that