Molecular Mass

yup sure
(CHOH)3(H)4
try this and compare it with
((CHOH)3(H)4) which gives correct answer…

Plz someone give algorithm for this problem. I have tried many times but fails

Plz someone give algorithm for this problem. I have tried many times but fails

I don’t know how to make computer understand whether last digit if encountered in chemical formulae is to be multiplied for entire formula or preceding group only.

For eg:

(CHOH)3(H)4 // here 4 to be multiplied only with H

((CH)2(OH2H)(C(H))O)4 // here 4 to be multiplied for entire whole formulae

1 Like

Maybe using use another stack would help… push pop values u need according to brackets

now this can be done by using some rules…

#0) push a zero before doing anything…
#1) push a “0” to stack on getting “(” (while iterating to right side)
#2) move till you get “)” and then check next right element… if it is a number take it into a temp variable… otherwise temp=1;
#3) start pop back and for each element you get pop last element and add value to it and again push it till you get “(”
#4) on getting “(” (while iterating left(popping))
#pop two elements from it and add the last element to second last element and again push second last element to stack…

Here is an example…

Click to view

##( (16)2 (10)3)2
then stack would be like

  1. 0 (push a zero before starting)
  2. 0 0 (#1st bracket)
  3. 0 0 0 (#2nd bracket)
  4. 0 0 0 (temp=2 on reaching “)” )
  5. 0 0 16 (keep popping adding pushing elements values inside it till “(” while moving left side)
  6. 0 (pop two on reaching “(” while moving left side )
  7. 0 32 (do this immediately after 6th step)
  8. 0 32 0 (on reaching bracket “(” before 10)
  9. 0 32 0 (temp=3 on reaching “)”)
  10. 0 32 10 (keep popping adding pushing elements values inside it till “(” while moving left side)
  11. 0 (pop two on reaching “(” while moving left side )
  12. 0 62
  13. (pop two on reaching “(” while moving left side )
    13)124 (do this immediately)
    the stack with formula is empty hence the only element left in stack is the answer

#HERE is my accepted solution in case you need reference…

1 Like

#HERE is my accepted solution in case you need reference…

hehe btw I liked your name… “code man” :smiley:

hehe btw I liked your name… “code man” :smiley:
and do ask if u dont understand anything… :slight_smile:

1 Like

Here I have started rule’s number from 0 as everything in coding starts from 0 and even we have 0th law of thermodynamics :slight_smile:

no reply for such a big answer :frowning: !!

thanks bro I forgot i liked ur answer i have not implemented yet.
Can u plz answer my next question “Shocked !!”(title) question mentioned on list (discussion)

okay welcome :slight_smile:

while thinking too much I have few doubts:

  1. I tried without zero then why its not working
  2. 3rd point in ur algorithm im not clear. Can u plz implement for this two contrast egs:-

((CH)2(OH2H)(C(H))O)3

(CHOH)3(H9H)3

I’m only not understanding when digit is encountered it will be multiplied with entire formula or only preceding group how to code this part.

have you seen my code ?? It would be helpful if u see my answer and code simultaneously…
At this time I am on a live contest so ll reply u later…

do{
flag=true;

            t=str.top();
            str.pop();
            
            switch(t){
                case '(': 
                            v=val.top();
                            val.pop();
                            v=(v*temp)+val.top();
                            break; 
                case 'C':
                            v=val.top();
                            v+=12*temp2;
                            break;
                case 'H':  
                            v=val.top();
                            v+=1*temp2;
                            break;
                case 'O':
                            v=val.top();
                            v+=16*temp2;
                            break;
                default :
                            flag=false; // make it false if t is a number
                            temp2=t-'0';    
            }
            
            if(flag){ // if t was not a number
                temp2=1;
                val.pop();
                val.push(v);
            }
            
        }  
        while(t!='(');

this is what my third point says @code_man

actually this contains 3rd and 4th point

initial value of temp2 is 1…