0
0
DSA Pythonprogramming~20 mins

Infix to Postfix Conversion Using Stack in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Infix to Postfix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of postfix expression from given infix
What is the output postfix expression after converting the infix expression (A+B)*C using stack-based infix to postfix conversion?
DSA Python
def infix_to_postfix(expression):
    precedence = {'+':1, '-':1, '*':2, '/':2, '^':3}
    stack = []
    output = []
    for char in expression:
        if char.isalpha():
            output.append(char)
        elif char == '(': 
            stack.append(char)
        elif char == ')':
            while stack and stack[-1] != '(': 
                output.append(stack.pop())
            stack.pop()
        else:
            while stack and stack[-1] != '(' and precedence.get(stack[-1], 0) >= precedence.get(char, 0):
                output.append(stack.pop())
            stack.append(char)
    while stack:
        output.append(stack.pop())
    return ''.join(output)

print(infix_to_postfix('(A+B)*C'))
A"AB+*C"
B"ABC+*"
C"AB+C*"
D"A+BC*"
Attempts:
2 left
💡 Hint
Remember that operators inside parentheses are handled first and multiplication has higher precedence than addition.
Predict Output
intermediate
2:00remaining
Postfix output for complex infix with multiple operators
What is the postfix expression output for the infix expression A+B*C-D/E using stack-based infix to postfix conversion?
DSA Python
def infix_to_postfix(expression):
    precedence = {'+':1, '-':1, '*':2, '/':2, '^':3}
    stack = []
    output = []
    for char in expression:
        if char.isalpha():
            output.append(char)
        elif char == '(': 
            stack.append(char)
        elif char == ')':
            while stack and stack[-1] != '(': 
                output.append(stack.pop())
            stack.pop()
        else:
            while stack and stack[-1] != '(' and precedence.get(stack[-1], 0) >= precedence.get(char, 0):
                output.append(stack.pop())
            stack.append(char)
    while stack:
        output.append(stack.pop())
    return ''.join(output)

print(infix_to_postfix('A+B*C-D/E'))
A"ABC*+DE/-"
B"AB+CD*-E/"
C"-/ED+*CBA"
D"AB+C*DE/-"
Attempts:
2 left
💡 Hint
Remember multiplication and division have higher precedence than addition and subtraction.
🔧 Debug
advanced
2:00remaining
Identify the error in postfix conversion code
Given the following code snippet for infix to postfix conversion, what error will it raise when run with input A+B?
DSA Python
def infix_to_postfix(expression):
    precedence = {'+':1, '-':1, '*':2, '/':2}
    stack = []
    output = []
    for char in expression:
        if char.isalpha():
            output.append(char)
        elif char == '(': 
            stack.append(char)
        elif char == ')':
            while stack[-1] != '(': 
                output.append(stack.pop())
            stack.pop()
        else:
            while stack and stack[-1] != '(' and precedence[stack[-1]] >= precedence[char]:
                output.append(stack.pop())
            stack.append(char)
    while stack:
        output.append(stack.pop())
    return ''.join(output)

print(infix_to_postfix('A+B'))
AKeyError: '('
BIndexError: list index out of range
CTypeError: '>=' not supported between instances of 'str' and 'int'
DNo error, outputs 'AB+'
Attempts:
2 left
💡 Hint
Check how the code handles the '(' character in the stack during precedence comparison.
🧠 Conceptual
advanced
2:00remaining
Operator precedence handling in infix to postfix conversion
In infix to postfix conversion using a stack, why is it important to pop operators from the stack when the incoming operator has lower or equal precedence than the operator on the stack top?
ATo delay the evaluation of operators until the end of the expression
BTo reverse the order of operands in the postfix expression
CTo remove all operators from the stack immediately after reading them
DTo ensure operators with higher precedence are applied before lower precedence ones in the postfix expression
Attempts:
2 left
💡 Hint
Think about how postfix expressions represent the order of operations.
🚀 Application
expert
2:00remaining
Postfix expression length after conversion
Given the infix expression ((A+B)*(C-D))/E, how many characters will the resulting postfix expression have after conversion using stack-based infix to postfix conversion?
A7
B9
C11
D13
Attempts:
2 left
💡 Hint
Count all operands and operators in the postfix expression after conversion.