Molecular Mass

by following code line by line im getting required output but im not able to generalize the logic behind ur solution so that I can code it myself and don’t have to try and run for every doubtful input. Can u tell me at pseudo code

pseudo code for whole question or for a specific part ?

For whole if its possible and u didn’t give up, bcoz now im getting obsessed and sleep only after getting AC

If you want step by step code then I have given you link of soln in c++
which is neat enough to understand and If you wanna understand step by step state(values) of stack then just add a print statements in my code and you ll get it…

I didn’t understood the difference between pseudo code and my c++ code if you already know c++ language…

pseudo I mean English like statements in sequence similar to one u gave before

should I repeat if I gave them before ??

just ask which part of that u didn’t understood…

I think I have only skipped one step i.e. how to get those “16” and “10” in above example…
the specification for that is the code i just gave about 30 mins ago

  1. for all characters other than ‘)’ – pushing

  2. for ‘)’ – pop till ‘(’
    while poping – add weight of each element store it in sum(say)

  3. check if next to ‘)’ is digit or not if digit – multiply it with sum
    is this logic right similar to urs.

eg-
((OH)2(C(H))O)3

So first sum = (16+1)*2=34

next popped will be H= 1

next popped will be C= 12

next popped will be O= 16 (but here after ‘)’ there is a digit hence 16*3=48)

therefore final weight comes to be = 34+1+12+16+48

which is wrong that last 3 should be multiplied with whole formula mass

but how to catch this part in code??? :frowning:

Oh my god so many comments here.

@code_man , what you’re calling pseudo code is called “stating the algorithm.” not “pseudo-code”. Thats why he got confused.

So, what exactly is the current requirement @code_man , what do you need? Pseudo code, or something else?

1 Like

maintain value of whole bracket in another stack of integer till you don’t reach its matching bracket and multiply that whole value with 3 when you reach “)”

you have to keep adding 1+12+16 in an integer of stack…
how do that ?? that is what my answer describes…

whenever I find ‘)’ can I identify its corresponding ‘(’ bracket. Is there any way for recognizing corresponding ‘(’ bracket for ‘)’.

Bcoz if that is possible it will become very easy to identify the digit following ‘)’ to be multiplied with which group.

Since you did not find the matching ‘)’ of the initial ‘(’, you maintain a cumulative sum of values till the corresponding ‘)’ is encountered.

but at the end stack position is like this:

=============

( | O | | |

and ‘)’ is encountered after O in input so it will pop O from stack and also below is the ‘(’ bracket which matches hence 16 gets multiplied by 3