-
Notifications
You must be signed in to change notification settings - Fork 8
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 #13
Comments
excuting my_stuck.pop** will remove the last element of the list, in this case Serveus. |
|
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) - initial state of the stack by executing my_stack.pop() for the 1st time- we have to eliminate the lastly added element on the stack in this case "Severus", by executing my_stack.pop() for the 2nd time-we have to follow the same procedure, however in this case on top of the stack is "Ron", so we eliminate this element and now our stock should like like this ["Draco", "Harry", "Hermione"] Lastly, we execute my_stack.append("Voldemort") - so by this we are adding on top of the stack "Voldemort", and the stack has to contain the following 4 elements |
|
my_stack = deque ( ) Now my_stack is ['Draco', 'Harry', 'Hermione', 'Ron']. my_stack.pop( ) my_stack.append("Voldemort") So, my_stack after is ['Draco', 'Harry', 'Hermione', 'Voldemort'] . |
my_stack=deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) my_stack.pop() my_stack.pop() my_stack.append("Voldemort") |
from collections import deque Characters_stack=deque(["Draco", "Harry", "Hermione", "Severus"]) Characters_stack.pop() #it removes "Severus", the last element in the stack. Characters_stack.pop() #it applies again to the last element and consequently it removes "Ron". Characters_stack.append("Voldemort") #it adds a new element print(Characters_stack) Characters_stack=deque(["Draco", "Herry", "Hermione", "Voldemort"]) |
1 from collections import deque In line 3, "Severus" is removed from the stack. The stack now contains "Draco", ”Harry", "Hermione", and "Ron". |
my_stack = deque(["Voldemort", "Hermione", "Ron", "Severus"]) #final state. |
|
harry_potter_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
|
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) my stack.pop () # i Remove Severus, the element on the top of my stack my stack.pop () # i Remove Ron My_stack.append (“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 of my_stack after the execution of each of the following operations: my_stack.pop(), my_stack.pop(), my_stack.append("Voldemort"). First we create a new stack: my_stack = deque() print(my_stack) Result: ['Draco', 'Harry', 'Hermione', 'Ron', 'Severus'] Since a stack is a particular list seen from bottom to top: my_stack.pop() --> this first operation would remove "Severus", since it's the last item added ['Draco', 'Harry', 'Hermione', 'Ron', 'Voldemort'] |
from collections import deque After the first characters_stack.pop the last element of the stack is cancelled, after the second the previous one. |
HPCharacters_stack = deque ( ) HPCharacters_stack.pop( ) HPCharacters_stack.pop( ) HPCharacters_stack.append("Voldemort") print (HPCharacters_stack) |
my_hp_stack = deque() print (my_hp_stack) Output ["Draco", "Harry", "Hermione", "Voldemort"] my_hp_stack.pop() # removes the element on top (stacks are seen from bottom to top) print (my_hp_stack) |
from collections import deque my_stack.pop() # removes the last element "Severus" my_stack.pop() # removes the last element "Ron" my_stack.append("Voldemort") #adds a new element "Voldemord" on the top of the stack |
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: