Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an operator onto the stack.
DSA Python
stack.[1](ch) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' which is not a Python list method.
✗ Incorrect
In Python, to add an element to the end of a list (used as a stack), we use append.
2fill in blank
mediumComplete the code to pop the top element from the stack.
DSA Python
top = stack.[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'remove' which removes by value, not by position.
✗ Incorrect
To remove and get the last item from a list (stack), use pop().
3fill in blank
hardFix the error in the precedence function to return correct precedence for '+' operator.
DSA Python
if op == '[1]': return 1
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing '+' with '-' or '*'.
✗ Incorrect
The '+' operator has precedence 1 in this function.
4fill in blank
hardFill both blanks to correctly check operator precedence and decide stack pop.
DSA Python
while stack and precedence(stack[-1]) [1] precedence(ch): postfix += stack.[2]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>='.
Using 'append' instead of 'pop'.
✗ Incorrect
We pop from stack while top has higher or equal precedence, so use '>=' and pop().
5fill in blank
hardFill all three blanks to correctly handle closing parenthesis in the stack.
DSA Python
while stack and stack[-1] != [1]: postfix += stack.[2]() stack.[3]() # Remove '('
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ')' instead of '(' for comparison.
Using append instead of pop.
✗ Incorrect
We pop until '(' is found, then pop '(' itself. So use '(' for comparison and pop() to remove.