Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Explain this Infix to Postfix code briefly into explanation and Flowchart presen

ID: 3719069 • Letter: E

Question

Explain this Infix to Postfix code briefly into explanation and Flowchart presentation (Python 3)

class Stack: def init_ _(self) self.items = [] self.length 0 def push (self, val) self.items.append (val) self.length 1 def pop(self) if self.empty): return None self.length1 return self.items .pop) dlef size (self) return self.length def peek(self) if self.empty): return None return self.items [0] def empty(self) return self. length = 0 def tr (self) return str (self.items) precedence precedence [ ' * ' ] = 3 precedence-3 precedenceI'+'-2 precedence--2 precedence''] - 1 def convert (expression) print(__convert (expression.split O)) dlef convert (tokens): postfix -[1 opstack-Stack ) or token in tokens if token.isidentifier: postfix.append (token) opstack.push (token) while True: elif tokenC': elif token'': tempopstack pop) if temp is None or temp' brea elif not temp.isidentifier postfix.append (temp) else: # must be operator if not opstack.empty): temp opstack.peek) while not opstack .empty() and precedence [temp] precedence [token] and token. isidentifier(): >= postfix.append (opstack po) temp opstack.peek (0 opstack-push (token) while not opstack.empty) postfix.append (opstack pop O) return postfix s input ( 'Enter Infix expression : convert (s) ' ) ;

Explanation / Answer

First Let me tell how the algorithm works:

Step 1: If u encounter an operand(i.e like a, b,c.....)U have to write it or append it to postfix expression

Step 2: (i)If u encounter an operator push it on to the stack either if there is no operator or If the precedence of the newly inserting operator should be less than already present one.

(ii)If u encounter that already present operator is less than the newly inserting operator, Then we have to

pop the already present operator and append it to the postfix expression

Step 3: If we encounter ')' we need to pop everything until we encounter '('

That's it Now u learnt the underlying Algorithm!Great!

Now let's Examine the Above code

Code:

we know that function is started by defining "def" keyword

def_init_ function: It is like a constructor for a class.Invoked During object Creation

And the remaining all functions are user Defined for his necessity

And below He has supplied all the precedence values- Precedence with value '1' has higher priority and so on

And now the convert function:

He used split function what it does:

split or break up a string and add the data to a string array using a defined separator.

Then he took a postfix array to store postfix expression

Then he called Stack()-This create the object for the stack class.This By default calls the constructor i.e init function and initialises the default values i.e Empty stack and length is 0;

Now in for loop going through entire Infix Expression:

First, he is checking whether the token is an identifier.

If it is an identifier.Just append it to the postfix expression

Then if it is an open brace( '(' ).Just push it on to the stack, Initially, the stack is empty.

if we encounter ')' (close brace)

Just pop everything on the stack until we encounter '(' (open brace)

While popping everything from the stack when we encounter operators ..We just append them to the postfix expression

And in the last else statement

we are using peek function.Peek function makes to point to top of the stack

And now we are checking whether the top of the stack operator and newly inserting operator precedences

If the precedence of the newly inserting operator is less than already present one.We push the new operaor on to the stack.

else

we pop the old operator and append the operator to postfix

And After all, we return the postfix expression

Hope it would help .. Appreciation would make me satisfied.

Happy learning/-

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote