Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element to the top of the stack.
DSA Python
stack = [] stack.[1](5) print(stack)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of append removes an element instead of adding.
Using remove or insert changes the wrong part of the list.
✗ Incorrect
Use append to add an element to the top of the stack (end of the list).
2fill in blank
mediumComplete the code to remove the top element from the stack.
DSA Python
stack = [1, 2, 3] top = stack.[1]() print(top, stack)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using append adds an element instead of removing.
Using remove deletes a specific value, not the last element.
✗ Incorrect
Use pop to remove and return the last element, which is the top of the stack.
3fill in blank
hardFix the error in the code to check if the stack is empty.
DSA Python
stack = [] if len(stack) [1] 0: print('Stack is empty') else: print('Stack has elements')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' checks for non-empty instead of empty.
Using '>' or '<' may give wrong results for empty stack.
✗ Incorrect
Check if length equals zero to confirm the stack is empty.
4fill in blank
hardFill both blanks to create a stack and add two elements.
DSA Python
stack = [1] stack.[2](10) stack.append(20) print(stack)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} creates a dictionary, not a list.
Using set() creates an unordered collection, not suitable for stack.
✗ Incorrect
Use [] to create an empty list as a stack and append to add elements.
5fill in blank
hardFill all three blanks to pop the top element and print the stack.
DSA Python
stack = [5, 10, 15] top = stack.[1]() print('Popped:', top) print('Stack now:', stack[2]stack[3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes an error.
Using other methods than pop to remove the top element.
✗ Incorrect
Use pop() to remove the top element. The print shows the stack plus its reversed version using + [::-1].