0
0
Intro to Computingfundamentals~10 mins

Stacks (last-in, first-out) in Intro to Computing - Interactive Code Practice

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

Complete the code to add an item to the top of the stack.

Intro to Computing
stack = []
stack.[1]('apple')
Drag options to blanks, or click blank then click option'
Aremove
Bpop
Cappend
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' which removes an item instead of adding.
Using 'remove' which deletes a specific item.
Using 'insert' which adds at a specific position, not necessarily the top.
2fill in blank
medium

Complete the code to remove the top item from the stack.

Intro to Computing
stack = ['apple', 'banana', 'cherry']
top_item = stack.[1]()
Drag options to blanks, or click blank then click option'
Apop
Bremove
Cappend
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'append' which adds an item instead of removing.
Using 'remove' which deletes a specific item by value.
Using 'insert' which adds an item at a position.
3fill in blank
hard

Fix the error in the code to check if the stack is empty.

Intro to Computing
stack = []
if len(stack) [1] 0:
    print('Stack is empty')
Drag options to blanks, or click blank then click option'
A!=
B==
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which checks if stack is not empty.
Using '>' which checks if stack has items.
Using '<=' which includes zero and negative numbers (not relevant here).
4fill in blank
hard

Fill both blanks to create a stack with numbers 1 to 5 and remove the top item.

Intro to Computing
stack = [[1] for num in range(1, 6)]
top = stack.[2]()
Drag options to blanks, or click blank then click option'
Anum
Bpop
Cappend
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'append' instead of 'pop' to remove the top item.
Using 'number' which is not defined.
Using 'num' incorrectly outside the comprehension.
5fill in blank
hard

Fill all three blanks to create a stack from letters, add 'z', and then remove the top item.

Intro to Computing
stack = [[1] for [2] in ['a', 'b', 'c']]
stack.[3]('z')
top = stack.pop()
Drag options to blanks, or click blank then click option'
Aletter
Bx
Cappend
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the comprehension.
Using 'pop' instead of 'append' to add an item.
Using undefined variable names.