comp-think/2022-2023

Lecture "Organising information: ordered structures", exercise 3

essepuntato opened this issue · 16 comments

Consider to have a queue obtained by processing, one by one, the elements included in the list of the first exercise, i.e. my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]). Describe the status of my_queue after the execution of each of the following operations: my_queue.popleft(), my_queue.append("Voldemort"), my_queue.popleft().

harry_potter_queue = deque ()
harry_potter_queue.append("Draco")
harry_potter_queue.append("Harry")
harry_potter_queue.append("Hermione")
harry_potter_queue.append("Ron")
harry_potter_queue.append("Severus")
harry_potter_queue.popleft()
# "Draco" was returned and then removed from the left-most part of the queue, now the queue is ["Harry", "Hermione", "Ron", "Severus"]
harry_potter_queue.append("Voldemort")
# "Voldemort" was added to the end of the queue, now the queue is ["Harry", "Hermione", "Ron", "Severus", "Voldemort"]
harry_potter_queue.popleft()
# "Harry" was returned and then removed from the left_most part of the queue, now the queue is ["Hermione", "Ron", "Severus", "Voldemort"]

by calling my_queue.popleft() first item of queue will be returned and then will be removed
by calling my_queue.append("Voldemort") the "Voldemort" will be added to end of the queue

  1. queue's status after first my_queue.popleft(): deque(['Harry', 'Hermione', 'Ron', 'Severus'])
  2. queue's status after my_queue.append("Voldemort"): deque(['Harry', 'Hermione', 'Ron', 'Severus', 'Voldemort'])
  3. queue's status after 2nd my_queue.popleft(): deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])

image

from collections import deque

my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])

my_queue.popleft() # deque(['Harry', 'Hermione', 'Ron', 'Severus']) <- "Draco" gone from the beginning of the queue
my_queue.append("Voldemort") # deque(['Harry', 'Hermione', 'Ron', 'Severus', 'Voldemort']) <- "Voldemort" appeared at the end of the queue
my_queue.popleft() # deque(['Hermione', 'Ron', 'Severus', 'Voldemort']) <- "Harry" gone from the beginning of the queue

Friends_queue = deque()
Friends_queue.append("Draco")
Friends_queue.append("Harry")
Friends_queue.append("Hermione")
Friends_queue.append("Ron")
Friends_queue.append("Severus")
Friends_queue.popleft()
#based on the FIFO strategy, "Draco" has been removed from the queue
print(Friends_queue)
Friends_queue.append("Voldemort")

"Voldemort" has been added to the queue

print(Friends_queue)
Friends_queue.popleft()
#based on the FIFO strategy, "Harry" has been removed from the queue
print(Friends_queue)

from _collections import deque


def test_my_stack(my_list, expected):
    if my_list == expected:
        return True
    else:
        return False


my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.popleft()    # Draco is removed
my_stack.append("Voldemort")      # Voldemort is added at the end of the query
my_stack.popleft()       # Harry is removed
print(my_stack)
print(test_my_stack(my_stack, deque(["Hermione", "Ron", "Severus", "Voldemort"])))
from collections import deque

my_queue = deque(["Harry", "Draco", "Hermione", "Ron", "Severus"])
my_queue.popleft()
my_queue.append("Voldemort")
my_queue.popleft()
print(my_queue)

from collections import deque

my_queue = deque ()
my_queue.append("Harry")
my_queue.append("Draco")
my_queue.append("Hermione")
my_queue.append("Ron")
my_queue.append("Severus")

print(my_queue)

my_queue.popleft()
print(my_queue)

my_queue.append("Voldemort")
print(my_queue)

my_queue.popleft()
print(my_queue)

OUTPUT(s):

deque(['Harry', 'Draco', 'Hermione', 'Ron', 'Severus'])
deque(['Draco', 'Hermione', 'Ron', 'Severus'])
deque(['Draco', 'Hermione', 'Ron', 'Severus', 'Voldemort'])
deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])

deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])

deque(["Hermione", "Ron", "Severus", "Voldemort"])

HP_queue = deque()
HP_queue.append("Draco")
HP_queue.append("Harry")
HP_queue.append("Hermione")
HP_queue.append("Ron")
HP_queue.append("Severus")
print(HP_queue)
HP_queue.popleft()
HP_queue.append("Voldemort")
HP_queue.popleft()
print(HP_queue)

output: deque(['Hermione', 'Ron', 'Severus', 'Voldemort'])

from collections import deque
my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_queue.popleft()
# "Draco", the first item added to the list, has been removed
my_queue.append("Voldemort")
# "Voldemort" has been added after the last item of the list
my_queue.popleft()
# "Harry", now being the first item of the list, has been removed
print(my_queue)
# now my_queue contains: deque(["Hermione", "Ron", "Severus", "Voldemort"])