-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lecture "Organising information: ordered structures", exercise 2 #15
Comments
The status of my_stack at the and of all the operations is: Draco, Harry, Hermione, Voldemort |
The status of the my_stack Will be: Draco, Harry, Hermione, Voldemort. |
|
|
The two commands |
|
|
|
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) my_stack.pop() stack becomes (["Draco", "Harry", "Hermione", "Ron"]). my_stack.pop() stack becomes (["Draco", "Harry", "Hermione"]). my_stack.append(Voldemort). stack becomes (["Draco", "Harry", "Hermione", "Voldemort"]). |
from collections import deque my_stack = deque (["Draco", "Harry", "Hermione", "Ron", "Severus"]) deque(['Draco', 'Harry', 'Hermione', 'Voldemort']) |
|
from collections import deque my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) my_stack.pop() # remove the rightmost item my_stack.append("Voldemort") # add a new entry to the right side |
Visualising it like a stack: my_stack my_stack.pop() my_stack.pop() my_stack.append("Voldemort") |
INPUT my_stack.pop() my_stack.pop() my_stack.append("Voldemort") OUTPUT |
The method .pop() removes the item on the top of the stack, that is the last item added in the stack -> my_stack.pop() my_stack.pop() The method .append() allows one to add an item on the top of the stack -> my_stack.append("Voldemort") |
|
At the top of our stack is Severus and Draco is at the bottom. Following the FILO principle. Giving us the final stack as - |
deque(['Draco', 'Harry', 'Hermione', 'Voldemort']) |
|
|
Consider to have a stack obtained by processing, one by one, the elements included in the list of the first exercise, i.e.
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
. Describe the status ofmy_stack
after the execution of each of the following operations:my_stack.pop()
,my_stack.pop()
,my_stack.append("Voldemort")
.The text was updated successfully, but these errors were encountered: