2018-2019
2018-2019 copied to clipboard
Lecture "Organising information: ordered structures", exercise 2
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")
.
excuting my_stuck.pop** will remove the last element of the list, in this case Serveus. my_stack.pop() again remove "Ron" from the stack. my_stack.append("Voldemort") will add "Volemort" to the right of the stack. so the final output of the stack my_stack will be deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
# currently my_stack contains five elements:
# "Severus"])
# "Ron",
# "Hermione",
# "Harry",
# deque(["Draco",
print(my_stack) # check my stack looks right
my_stack.pop() # this removes the element on top of the stack
# currently my_stack contains four elements:
# "Ron"])
# "Hermione",
# "Harry",
# deque(["Draco",
my_stack.pop() # this, again, removes the element on top of the stack
# currently my_stack contains three elements:
# "Hermione"])
# "Harry",
# deque(["Draco",
my_stack.append("Voldemort") # this adds a new item ot the stack, on top
# currently my_stack contains four elements:
# "Voldemort"])
# "Hermione",
# "Harry",
# deque(["Draco",
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", so after that the stack will look like this ["Draco", "Harry", "Hermione", "Ron"]
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 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 of my_stack after the execution of each of the following operations:
# my_stack.pop(), my_stack.pop(), my_stack.append("Voldemort").
from collections import deque
Grinwald_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
Grinwald_stack.pop()
#this operation modifies the stack "Grinwald_stack" removing the last value "Severus"
#currently the stack "Grinwald_stack" contains four elements
#deque(["Draco", "Harry", "Hermione", "Ron"])
Grinwald_stack.pop()
#this operation modifies the stack removing the (now new) last value "Ron"
#currently the stack "Grinwald_stack" contains three elements
#deque(["Draco", "Harry", "Hermione"])
Grinwald_stack.append("Voldemort")
#this operation modifies the stack "Grinwald_stack" adding at the very top the value "Voldemort"
#currently the stack "Grinwald_stack" contains three elements
#deque(["Draco", "Harry", "Hermione", "Voldermort"])
print(Grinwald_stack)
deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
my_stack = deque ( ) my_stack = (["Draco", "Harry", "Hermione", "Ron", "Severus"]) my_stack.pop( ) output is 'Severus' #"Severus" is extracted from my_stack.
Now my_stack is ['Draco', 'Harry', 'Hermione', 'Ron'].
my_stack.pop( ) output is 'Ron' #'Ron' is extracted from my_stack. #Now my_stack is ['Draco', 'Harry', 'Hermione'].
my_stack.append("Voldemort") print (my_stack) output = ['Draco', 'Harry', 'Hermione', 'Voldemort']
So, my_stack after is ['Draco', 'Harry', 'Hermione', 'Voldemort'] .
my_stack=deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop() my_stack=deque(["Draco", "Harry", "Hermione", "Ron"])
my_stack.pop() my_stack=deque(["Draco", "Harry", "Hermione"])
my_stack.append("Voldemort") my_stack=deque(["Draco", "Harry", "Hermione", "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 currently contains 4 elements ["Draco", "Herry", "Hermione", "Ron"]
Characters_stack.pop() #it applies again to the last element and consequently it removes "Ron". #Characters_stack is now composed by 3 elements ["Draco","Herry","Hermione"]
Characters_stack.append("Voldemort") #it adds a new element
print(Characters_stack)
Characters_stack=deque(["Draco", "Herry", "Hermione", "Voldemort"])
1 from collections import deque 2 harry_potter_und_ein_stein = deque(["Draco","Harry","Hermione","Ron","Severus"]) 3 harry_potter_und_ein_stein.pop() 4 harry_potter_und_ein_stein.pop() 5 harry_potter_und_ein_stein.append("Voldemort")
In line 3, "Severus" is removed from the stack. The stack now contains "Draco", ”Harry", "Hermione", and "Ron". In line 4, "Ron" is removed from the stack. The stack now contains "Draco", "Harry", and "Hermione". In line 5, "Voldemort" is added to the stack on the right. The stack now contains "Draco", "Harry", "Hermione", and "Voldemort".
- my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
- my_stack.pop #remove the fist element, in this case "Draco" my_stack = deque(["Harry", "Hermione", "Ron", "Severus"])
- my_stack.pop #remove the fist element, in this case "Harry" my_stack = deque(["Hermione", "Ron", "Severus"])
- my_stack.append ("Voldemort") #add an element on the top of the stack
my_stack = deque(["Voldemort", "Hermione", "Ron", "Severus"]) #final state.
from collections import deque
#first of all I need to import the construct _deque_ from the module _collections_
potter_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
#I've created a stack containing five different strings, ordered alphabetically. In this case, "Severus" is the last on top of it.
potter_stack.pop()
#By using the _.pop_ method I'm removing the variable on top of the pile, in this case "Severus"
potter_stack.pop()
#Now poor "Ron" is going to be removed.
potter_stack.append("Voldemort")
#And, through the _.append_ method, evil "Voldemort" is going to be added on top.
print(potter_stack)
#Printing the result on screen I can see that my stack now contains, in order: "Draco", "Harry", "Hermione", "Voldemort".
harry_potter_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) #stack created with elements in the order I provided harry_potter_stack.pop() #take "Severus" out, since is the last element added harry_potter_stack.pop() #take "Ron" out, since is the second last element added harry_potter_stack.append("Voldemort") #add "Voldemort" Thus: 'Draco', 'Harry', 'Hermione', 'Voldemort'
from collections import deque
harry_potter_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
harry_potter_stack.pop()
harry_potter_stack.pop()
harry_potter_stack.append("Voldemort")
print(harry_potter_stack)
deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my stack.pop () # i Remove Severus, the element on the top of my stack my_stack = deque(["Draco", "Harry", "Hermione", "Ron"])
my stack.pop () # i Remove Ron my_stack = deque(["Draco", "Harry", "Hermione"])
My_stack.append (“Voldemort”) my_stack = 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 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() my_stack.append("Draco") my_stack.append("Harry") my_stack.append("Hermione") my_stack.append("Ron") my_stack.append("Severus")
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 my_stack.pop() --> this time would delete "Ron", which was added right before "Severus" my_stack.append("Voldemort") --> this would add a new item to the stack, so the final result will be:
['Draco', 'Harry', 'Hermione', 'Ron', 'Voldemort']
from collections import deque characters_stack = deque() characters_stack.append("Draco") characters_stack.append("Harry") characters_stack.append("Hermione") characters_stack.append("Ron") characters_stack.append("Severus") characters_stack.pop() characters_stack.pop() characters_stack.append("Voldemort") print(characters_stack)
After the first characters_stack.pop the last element of the stack is cancelled, after the second the previous one. After the characters_stack.pop operation the string "Voldemort" is added.
HPCharacters_stack = deque ( ) HPCharacters_stack = (["Draco","Harry","Hermione","Ron","Severus"]) #stack creation
HPCharacters_stack.pop( ) #it removes the element on top of the stack which is Severus
HPCharacters_stack.pop( ) #it removes the element on top of the stack which is Ron"
HPCharacters_stack.append("Voldemort") #it adds a new name
print (HPCharacters_stack) output = ['Draco', 'Harry', 'Hermione', 'Voldemort']
my_hp_stack = deque() my_hp_stack.append("Draco") my_hp_stack.append("Harry") my_hp_stack.append("Hermione") my_hp_stack.append("Ron") my_hp_stack.append("Severus")
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) my_hp_stack = (["Draco", "Harry", "Hermione", "Ron"]) my_hp_stack.pop() # removes the element on top my_hp_stack = (["Draco", "Harry", "Hermione"]) my_hp_stack.append("Voldemort") # add one element to the top
print (my_hp_stack) ["Draco", "Harry", "Hermione", "Voldemort"]
from collections import deque my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) # this passage creates a stack containing 5 names print(my_stack)
my_stack.pop() # removes the last element "Severus" print(my_stack)
my_stack.pop() # removes the last element "Ron" print(my_stack)
my_stack.append("Voldemort") #adds a new element "Voldemord" on the top of the stack print(my_stack)