Lecture "Organising information: ordered structures", exercise 2
Opened this issue · 21 comments
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")
.
- my_stack = deque(["Draco", "Harry", "Hermione", "Ron"])
- my_stack = deque(["Draco", "Harry", "Hermione"])
- my_stack = deque(["Draco", "Harry", "Hermione", "Voldemort"])
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop()
returns: Draco, Harry, Hermione, Ron
my_stack.pop()
returns: Draco, Harry, Hermione
my_stack.append( "Voldemort")
returns: Draco, Harry, Hermione, Voldemort
from collections import deque
my_stack = deque(["Draco", "Harry","Hermione", "Ron", "Severus"])
my_stack.pop()
my_stack.pop()
my_stack.append("Voldemort")
print(my_stack)
from _collections import deque # This imports deque
my_stack = deque() # This creates a new stack
my_stack.append("Draco") # Now we add the elements
my_stack.append("Harry")
my_stack.append("Hermione")
my_stack.append("Ron")
my_stack.append("Severus")
deque(['Draco', 'Harry', 'Hermione', 'Ron', 'Severus'])
my_stack.pop() # Now we execute the exercise
my_stack.pop()
my_stack.append("Voldemort")
print(my_stack)
deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
from _collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop() # returns deque(['Draco', 'Harry', 'Hermione', 'Ron'])
my_stack.pop() # returns deque(['Draco', 'Harry', 'Hermione'])
my_stack.append("Voldemort") # returns deque(['Draco', 'Harry', 'Hermione', 'Voldermort'])
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]).
After my_stack.pop()
, my_stack
would contain "Draco", "Harry", "Hermione", "Ron"
After again my_stack.pop()
my_stack
would contain "Draco", "Harry", "Hermione"
After my_stack.append("Voldemort")
, my_stack
would contain "Draco", "Harry", "Hermione", "Voldemort"
from collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop() #it removes the last string of the stack ("Severus") and return "Severus"
my_stack.pop() #it removes the last string of the stack ("Ron") and return "Ron"
my_stack.append("Voldemort") #it adds the string "Voldemort" at the end of the stack
print(my_stack) #it prints deque["Draco", "Harry", "Hermione", "Voldemort"]
status 1 - deque(["Ron", "Hermione", "Harry", "Draco"])
status 2 - deque(["Hermione", "Harry", "Draco"])
status 3 - deque(["Voldemort", "Hermione", "Harry", "Draco"])
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop() #current status: deque(["Draco", "Harry", "Hermione", "Ron"])
my_stack.pop() #current status: deque(["Draco", "Harry", "Hermione"])
my_stack.append("Voldemort") #current status: deque(["Draco", "Harry", "Hermione", "Voldermort"])
from collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop()
print(my_stack)
my_stack.pop()
print(my_stack)
my_stack.append("Voldemort")
print(my_stack)
# Output:
deque(['Draco', 'Harry', 'Hermione', 'Ron']) # the last item of the deque ("Severus") is returned and removed
deque(['Draco', 'Harry', 'Hermione']) # after deleting "Severus", the last item of the deque is "Ron", which is again returned and removed from my_stack
deque(['Draco', 'Harry', 'Hermione', 'Voldemort']) # The string "Voldemort" is added at the end of the deque
my_stack = deque(["Draco","Harry","Hermione","Ron","Severus"]):
my_stack.pop() #the last item is removed and the algorithm returns deque(["Draco","Harry","Hermione","Ron"])
my_stack.pop() #returns deque(["Draco","Harry","Hermione"])
my_stack.append("Voldemort") #addition of this item
print(my_stack) #it will print ["Draco","Harry","Hermione","Voldemort"]
from collections import deque
my_stack = deque(["Draco", "Harry","Hermione", "Ron", "Severus"])
my_stack.pop()
my_stack.pop()
my_stack.append("Voldemort")
print(my_stack)
OUTPUT:
deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
from collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop()
my_stack.pop()
my_stack.append("Voldemort")
print(my_stack)
deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
Initial stack: my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
1 - my_stack.pop(): my_stack = deque(["Draco", "Harry", "Hermione", "Ron"])
2 - my_stack.pop(): my_stack = deque(["Draco", "Harry", "Hermione"])
3 - my_stack.append("Voldemort"): my_stack = deque(["Draco", "Harry", "Hermione", "Voldemort"])
The stack will first eliminate the last element, then the second-to last, and then add the new one.
Each step will modify the stack as follows:
["Draco", "Harry", "Hermione", "Ron"]
then
["Draco", "Harry", "Hermione"]
and finally
["Draco", "Harry", "Hermione", "Voldemort"]
from collections import deque
my_stack = deque(["Draco", "Harry","Hermione", "Ron", "Severus"])
my_stack.pop()
my_stack.pop()
my_stack.append("Voldemort")
print(my_stack)
deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
from collections import deque
my_stack = deque(["Draco","Harry","Hermione","Ron","Severus"])
my_stack.pop()
my_stack.pop()
my_stack.append("Voldemort")
print(deque)
deque = (['Draco', 'Harry', 'Hermione', 'Voldemort'])
from collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop()
print(my_stack)
my_stack.pop()
print(my_stack)
my_stack.append("voldemort")
print(my_stack)
deque(['Draco', 'Harry', 'Hermione', 'Ron'])
deque(['Draco', 'Harry', 'Hermione'])
deque(['Draco', 'Harry', 'Hermione', 'voldemort'])
from collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop()
print(my_stack) #(['Draco', 'Harry', 'Hermione', 'Ron'])
my_stack.pop()
print(my_stack) #(['Draco', 'Harry', 'Hermione'])
my_stack.append("Voldemort")
print(my_stack) #(['Draco', 'Harry', 'Hermione', 'Voldemort'])
from collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop()
my_stack.pop()
my_stack.append("Voldemort")
print(my_stack)
deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])
my_stack:
after my_stack.pop() --> my_stack = deque (["Draco", "Harry", "Hermione", "Ron"])
after my_stack.pop() --> my_stack = deque (["Draco", "Harry", "Hermione"])
after my_stack.append("Voldemort") --> my_stack = deque (["Draco", "Harry", "Hermione", "Voldemort"])