in DS recategorized
5,036 views
0 votes
0 votes

Given the following prefix expression :

$^{*} + 3 + 3 ↑ 3 + 3 3 3$

What is the value of the prefix expression ?

  1. $2178$ 
  2. $2199$
  3. $2205$ 
  4. $2232$
in DS recategorized
5.0k views

2 Answers

3 votes
3 votes

prefix expression = ∗ + 3 + 3 ↑ 3+ 3 3 3

=>         * + 3 + 3 ^ 3 ( 3+ 3)  3    // here  ^  -> power operator.

=>         * + 3 + 3 ^ 3 6 3

=>        * + 3 + 3 (3^6) 3

=>        * + 3 + 3 729 3

=>        * + 3 ( 3 + 729 ) 3

=>        * + 3 732 3

=>        * ( 3 + 732) 3

=>        * 735 3

=>         (735 ) * 3

=>         2205  ( ans- C)


Method to follow -

Incase of  postfix expression, we push the element on seeing operand in to the stack and on seeing operator we pop top two elements from the stack and again push (pop2( 2nd poped) <operator> pop1(first popped) back into the stack.

Just like postfix, apply the same logic incase of prefix too but, start from the last of prefix expression with one modification, on seeing an operator and  after doing 2 pop, we need to push here (1st pop <operator> 2nd pop). And rest everything is same as postfix. 

Here-  ∗ + 3 + 3 ↑ 3+ 3 3 3     // stack S = Empty.

push 3.

 ∗ + 3 + 3 ↑ 3+ 3 3 3          // S = 3

push 3

 ∗ + 3 + 3 ↑ 3+ 3 3 3         // S = 3 3

push 3         

∗ + 3 + 3 ↑ 3 + 3 3 3         // S = 3 3 3           

This time its an operator ( + ).

So, pop1=3, pop2 = 3, Now do push ( 3 + 3) => push (6)

∗ + 3 + 3 ↑ 3 + 3 3 3              // S = 6 3

push 3

∗ + 3 + 3 3+ 3 3 3              // S = 3 6 3

This time its an operator ( ^ ).

So, pop1=3, pop2 = 6, Now do push ( 3 ^ 6) => push (729)  // note here,  pop1 (3) < operator > pop2 (6).

∗ + 3 + 3 ↑ 3+ 3 3 3    // S = 729 3

push 3                        

∗ + 3 + 3 3+ 3 3 3      // S = 3 729 3

This time its an operator ( + ).

So, pop1=3, pop2 = 729, Now do push ( 3 + 729) => push (732)

∗ + 3 + 3 3+ 3 3 3    // S = 732 3

push 3                       

+ 3 + 3 3+ 3 3 3    // S=  3  732  3

This time its an operator ( + ).

So, pop1=3, pop2 = 732, Now do push ( 3 + 732) => push (735)

+ 3 + 3 3+ 3 3 3      // S = 725 3

This time its an operator ( * ).

So, pop1=735, pop2 = 3, Now do push ( 735 * 3) => push (2205)

Ans = 2205

edited by

4 Comments

r u following any rule?
0
0
@srestha, Is it clear now ??
0
0
Hi vijay, can you please tell me how do you if you want to take the split operand or one by one.

For example you have taken

333 as 3,3,3 in stack.

 

Consider this problem

++ 26 + - 1324

now how will you take 1324 is it one by one or 4,2,13 in the stack? Please explain
0
0

First of all, I hope you got the method to solve prefix.

Now coming to your point,

++ 26 + - 1324

here if we assume 1324 as 1 3 2 4 then we will be lacking with one operator at last. Just like balanced/unbalanced parentheses, the above assumtion will lead to wrong mathematical expression. So here ambiguity comes into picture. Here then total possibility can be -

1). + + 2 6 + - 1 3 24

2). + + 2 6 + - 1 32 4

3). + + 2 6 + - 13 2 4

4). + + 26 + - 1 3 2 4

now in this case (your example),  The question should mention all operands of the expression.

But here in the asked question, there is no ambiguity in assuming single digit operands.

hope it clears your doubt now.

0
0
0 votes
0 votes
c 2205

1 comment

how?
0
0

Related questions