Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push operands onto the stack.
DSA Python
if ch.isdigit(): stack.[1](int(ch))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of append() to add elements.
Using push() which is not a Python list method.
✗ Incorrect
In Python, list's append() method adds an element to the end of the list, which is used here to push onto the stack.
2fill in blank
mediumComplete the code to pop the top two operands from the stack.
DSA Python
op2 = stack.[1]()
op1 = stack.pop() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using append() which adds elements instead of removing.
Using remove() which removes by value, not by position.
✗ Incorrect
pop() removes and returns the last element from the list, which is the top of the stack.
3fill in blank
hardFix the error in the operation application line to correctly compute the result.
DSA Python
if ch == '+': res = op1 [1] op2
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' or '*' instead of '+'.
Using '/' which performs division.
✗ Incorrect
For addition, the operator '+' must be used between operands.
4fill in blank
hardFill both blanks to correctly handle multiplication and division operations.
DSA Python
elif ch == '*': res = op1 [1] op2 elif ch == '/': res = op1 [2] op2
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up operators like using '+' for multiplication.
Using '-' for division.
✗ Incorrect
Multiplication uses '*', division uses '/' operators between operands.
5fill in blank
hardFill all three blanks to complete the postfix evaluation function with correct stack operations and return.
DSA Python
def evaluate_postfix(expr): stack = [] for ch in expr: if ch.isdigit(): stack.[1](int(ch)) else: op2 = stack.[2]() op1 = stack.pop() if ch == '+': res = op1 + op2 elif ch == '-': res = op1 - op2 elif ch == '*': res = op1 * op2 elif ch == '/': res = op1 / op2 stack.append(res) return stack.[3]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove() which removes by value, not position.
Mixing append and pop incorrectly.
✗ Incorrect
Operands are pushed using append(), popped using pop(), and the final result is popped from the stack.