0
0
Data Structures Theoryknowledge~10 mins

Stack applications (expression evaluation, backtracking) in Data Structures Theory - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to push an element onto the stack.

Data Structures Theory
stack.[1](element)
Drag options to blanks, or click blank then click option'
Aremove
Bpop
Cpeek
Dpush
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'pop' instead of 'push' to add elements.
2fill in blank
medium

Complete the code to check if the stack is empty before popping.

Data Structures Theory
if not stack.[1]():
    stack.pop()
Drag options to blanks, or click blank then click option'
Ais_empty
Bpush
Cpeek
Dsize
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'peek' or 'size' which do not return a boolean for emptiness.
3fill in blank
hard

Fix the error in the expression evaluation code by completing the blank.

Data Structures Theory
while stack and precedence(stack[-1]) >= precedence([1]):
    output.append(stack.pop())
Drag options to blanks, or click blank then click option'
Acurrent_op
Bstack[-1]
Coutput[-1]
Dexpression[-1]
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the top of the stack instead of the current operator.
4fill in blank
hard

Fill both blanks to correctly implement backtracking with a stack.

Data Structures Theory
stack.[1](state)
while stack:
    current = stack.[2]()
Drag options to blanks, or click blank then click option'
Apush
Bpop
Cpeek
Dremove
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'peek' or 'remove' which do not add or remove elements properly.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps operators to their precedence if precedence is greater than 1.

Data Structures Theory
precedence_map = { [1]: [2] for [1], [2] in [3].items() if [2] > 1 }
Drag options to blanks, or click blank then click option'
Aop
Bprec
Coperators
Ditems
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'items' as a variable name or repeating the same variable for key and value.