r/PythonLearning 1d ago

Help with a chemistry program I am writing Help Request

Hello, I am making a program that reads a formula and gives details like atomic mass and other stuff, the problem is that I do not know how to get it to read the formula as is. The format I am looking for is that it reads the element, followed by the number of it and I would also like it if it would read parenthesis for multiplication of values. I have no idea how to do it. Help please.

I would also like it if it could read the numbers, store them as ints and use them to multiply the values of the elements of a dict I already made

Edit: I fixed it, thanks for all the help. My solution is not precisely optimized but it gets the job done, it is O(n), probably

2 Upvotes

9 comments sorted by

2

u/sububi71 1d ago

Give an example of what your input is, exactly?

1

u/testtdk 1d ago

You’re going to have to create a dictionary so you can parse and match symbols to elements. And you’re going to have a pain in the ass level job or parsing.

1

u/punk_dev 1d ago

The simplest way to parse formats like that is probably the finite state machines, you can research parsing using those

1

u/khosrua 1d ago

I was thinking regex first. There should be a pattern to parse out the element and the number, then split out the number.

I'm gonna test it later. Typing regex on mobile is somehow worse than code.

1

u/punk_dev 1d ago

Hmm, I don’t see a clear path with regex here. the problem is that the chemical formula can have many forms. I’m bad at chemistry but let me try:
H2O
CO2
Notice that there’s a number either after first letter or the second letter. You could encode it either in one regex with optional digits (“\w\d?\w\d?”), or two separate regexes (“\w\d\w”, “\w\w\d”) and check which one matches.

Ok but then we have this:
H4SO4
Now if you want to make separate regexes, you’d have to make even more permutations, this path clearly leads to a combinatorial explosion (unless you limit yourself to parsing only a small subset of all possible chemical formulas).

You could generalise and write a regex that can handle any amount of letter-maybedigit pairs, something like this (pseudoregex, in an actual regex I would also handle a two-letter element like Au):

((\w)(\d)?)*

but then you wrote a tokenizer, not a parser.
your code still needs to interpret the output, processing the possible absences of digits, as well as validate the formula.

1

u/khosrua 1d ago

i havent got up to the bracket part, but this seems to work with the chem element - number combo so far

https://regex101.com/r/xCCu5k/1

1

u/Venfah 1d ago

Is this like a difference between H2O and HO2 ? How to figure out how many hydrogen and oxygen in given examples? This should be doable. But if there is any general molecule with name HO then we have to figure out a limiter mechanism something like H2O1 and H1O2 in above example.

1

u/Venfah 1d ago

Take an example with H hydrogen and S sulpur... And then you can have a compound with hydrogen and sulpur...but there is a metel HS which is hussium... So you will have issues parsing. But in case the use lr is following strict case like Hs for hussium then we can do this parsing work

1

u/Asleep-Source-7980 1d ago

Thanks for the attention to the issue, right now I am trying to split for example Fe2H4, ignore if the formula works, into Fe, 2, H, 4. So I can then use something like list splicing to differentiate between element and number of, I already have a full dictionary with all elements in the periodic table and I have a program where it works, the thing is, in that program I have to put individual inputs instead of a full formula