Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element onto 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
Using remove which deletes a value
✗ Incorrect
To add an element to the top of the stack, we use the append method.
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) print(stack)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using append instead of pop
Using remove which deletes by value
✗ Incorrect
The pop method removes and returns 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 is not empty')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using != which checks for not empty
Using > or < which are incorrect here
✗ Incorrect
To check if the stack is empty, we compare its length to zero using ==.
4fill in blank
hardFill both blanks to create a stack and push an element.
DSA Python
stack = [1] stack.[2](10) print(stack)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which creates a dictionary
Using pop to add elements
✗ Incorrect
We create an empty list with [] and add an element using append.
5fill in blank
hardFill all three blanks to pop an element and check if stack is empty.
DSA Python
stack = [5, 6, 7] top = stack.[1]() if len(stack) [2] 0: print('Stack is empty after popping') else: print('Stack still has elements') print('Popped element:', [3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using append instead of pop
Using != instead of ==
Printing stack instead of popped element
✗ Incorrect
Use pop to remove the top element, check if length equals zero, and print the popped element stored in top.