2018-2019 icon indicating copy to clipboard operation
2018-2019 copied to clipboard

Lecture "Organising information: ordered structures", exercise 3

Open essepuntato opened this issue 6 years ago • 18 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().

essepuntato avatar Nov 23 '18 06:11 essepuntato

''' my_queue.popleft(): removes "Draco" from the queue. my_queue.append("Voldemort"): add "Voldemort" to the last index of the queue. my_queue.popleft(): removes "Harry" from the queue.

finally printing my_queue will look like deque(["Hermione", "Ron", "Severus","Voldemort"]) '''

hizclick avatar Nov 23 '18 13:11 hizclick

my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
# currently my_queue contains five elements:
# deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])

print(my_queue) # check my queue looks right

my_queue.popleft() # this removes the first element added
# currently my_queue contains four elements:
# deque(["Harry", "Hermione", "Ron", "Severus"])

my_queue.append("Voldemort") # this adds a new element to the queue
# currently my_queue contains five elements:
# deque(["Harry", "Hermione", "Ron", "Severus", "Voldemort"])

my_queue.popleft() # this removes the first element added
# currently my_queue contains four elements:
# deque(["Hermione", "Ron", "Severus", "Voldemort"])

delfimpandiani avatar Nov 23 '18 13:11 delfimpandiani

my_queue = dueue(["Draco", "Harry", "Hermione", "Ron", "Severus"]) contains five elements

  1. my_queue.popleft() removes the first element from the left, in this case "Draco" at the moment my_queue is composed by: deque(["Harry", "Hermione", "Ron", "Severus"])

  2. my_queue.append("Voldemort") adds the chosen element, in this case "Voldemord", in the queue at the moment my_queue is composed by: deque(["Harry", "Hermione", "Ron", "Severus", "Voldemord"])

  3. my_queue.popleft() removes the first element from the left, in this case "Harry" at the moment my_queue is composed by: deque(["Hermione", "Ron", "Severus", "Voldemord"])

Finally my_queue contains four elements: "Hermione", "Ron", "Severus", "Voldemord".

mchiaraf avatar Nov 23 '18 16:11 mchiaraf

my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])- initial state of the queue

By executing my_queue.popleft() - we remove "Draco" from the queue, rule of the queue FIFO - current state of the queue ["Harry", "Hermione", "Ron", "Severus"])

By my_queue.append("Voldemort") - we add "Voldemort" at the back of the queue, and now it looks like this -current state of the queue -["Harry", "Hermione", "Ron", "Severus", Voldemort"]

Next we execute once again my_queue.popleft() - we remove the 1st element on the left side, which in this case is "Harry"

So the final look of the queue should be ["Hermione", "Ron", "Severus", Voldemort"]

bluebell94 avatar Nov 23 '18 16:11 bluebell94

#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().

from collections import deque

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

Grinwald_queue.popleft() 
#this operation modifies the queue "Grinwald_queue" removing the first element "Draco"
#currently "Grinwald_queue" contains four elements:
# deque(["Harry", "Hermione", "Ron", "Severus"])

Grinwald_queue.append("Voldemort") 
#this operation modifies the queue "Grinwald_queue" adding the new element "Voldemort"
#currently "Grinwald_queue" contains five elements:
# deque(["Harry", "Hermione", "Ron", "Severus","Voldemort"])

Grinwald_queue.popleft()
#this operation modifies the queue "Grinwald_queue" removing the first element "Draco"
#currently "Grinwald_queue" contains four elements:
# deque(["Hermione", "Ron", "Severus","Voldemort"])

print (Grinwald_queue)

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

ilsamoano avatar Nov 23 '18 17:11 ilsamoano


my_queue = deque ( ["Draco", "Harry", "Hermione", "Ron", "Severus"] )
my_queue.popleft( )
```      #'Draco' is extracted from my_queue because the element on the left is supposed to be removed first.

print (my_queue)
deque ( ['Harry', 'Hermione', 'Ron', 'Severus'])

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

# 'Voldemort' is added at the end on the right.

my_queue.popleft( )
#'Harry' is extracted from my_queue because the element on the left is supposed to be removed first.

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

So; after the execution of each of the following operations, my_queue is ( ['Hermione', 'Ron', 'Severus', 'Voldemort'] ) .

simayguzel avatar Nov 23 '18 17:11 simayguzel

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

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

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

my_queue.popleft() my_queue=deque(["Hermione", "Ron", "Severus", "Voldemort"])

DavideApolloni avatar Nov 24 '18 10:11 DavideApolloni

1 from collections import deque 2 harry_potter_und_ein_stein = deque(["Draco","Harry","Hermione","Ron","Severus"]) 3 harry_potter_und_ein_stein.popleft() 4 harry_potter_und_ein_stein.append("Voldemort") 5 harry_potter_und_ein_stein.popleft()

In line 3, "Draco" is removed from the queue. The queue now contains "Harry", "Hermione", "Ron", and "Severus". In line 4, "Voldemort" is added to the queue on the right. The queue now contains "Harry", "Hermione", "Ron", "Severus", and "Voldemort". In line 5, "Harry" is removed from the queue. The queue now contains "Hermione", "Ron", "Severus", and "Voldemort".

SeverinJB avatar Nov 24 '18 11:11 SeverinJB

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

  • my_queue.popleft my_deque("Harry", "Hermione", "Ron", "Severus") #delete the first element of the stack, in this case "Draco"
  • my_queue.append("Voldemort") my_queue("Harry", "Hermione", "Ron", "Severus", "Voldemort") #add a new element at the end of the queue
  • my_queue.leftpop() my_queue("Hermione", "Ron", "Severus", "Voldemort") #remove the first left item, according to the FIFO rule.

MattiaSpadoni avatar Nov 24 '18 15:11 MattiaSpadoni

from collections import deque
#import deque from module collections as before
potter_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
#my queue has five strings, "Draco", "Harry", "Hermione", "Ron", "Severus"
potter_stack.popleft()
#the first element from the left ("Draco") has been removed from the queue
potter_stack.append("Voldemort")
#"Voldemort" has been added as the rightmost element (after "Severus")
potter_stack.popleft()
#the first element from the left ("Harry") has been removed from the queue, leaving, in order, "Hermione", "Ron", "Severus" and "Voldemort"
print(potter_stack)

friendlynihilist avatar Nov 24 '18 16:11 friendlynihilist

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

my_queue.popleft() #it removes the first inserted element #output my_queue (["Harry", "Hermione", "Ron", "Severus"]) my_queue.append("Voldemort") #it adds one element at the end of the queue #output my_queue (["Harry","Hermione","Ron","Severus","Voldemort"]) my_queue.popleft() #it removes the left-most element of the queue #my_queue became (["Hermione","Ron", "Severus","Voldemort"])

Totaro1996 avatar Nov 24 '18 18:11 Totaro1996

harry_potter_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) #creates stack harry_potter_queue.popleft() #takes "Draco" out since it is the first element of the queue harry_potter_queue.append("Voldemort") #adds "Voldemort" to the right of the queue harry_potter_queue.popleft() #takes "Harry" out Thus: 'Hermione', 'Ron', 'Severus', 'Voldemort'

from collections import deque

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

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

federicabologna avatar Nov 25 '18 10:11 federicabologna

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

my_queue.popleft () # Draco is remove, because in this case the first it’s the first out my_queue = deque([ "Harry", "Hermione", "Ron", "Severus"])

My_queque.append (“Voldemort”) #add Voldermort at the end of the queue my_queue = deque([ "Harry", "Hermione", "Ron", "Severus", “Voldemort”])

my_queue.popleft () # Harry is remove, because in this case the first it’s the first out my_queue = deque( "Hermione", "Ron", "Severus", “Voldemort”])

saraarmaroli avatar Nov 25 '18 15:11 saraarmaroli

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().

Let's first create the queue:

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

print(my_queue)

Result: ['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']

Since in a queue, the first element added is even the first that is deleted (FIFO strategy): my_queue.popleft() --> this operation would remove "Draco", because it was the first element added my_queue.append("Voldemort") --> this operation would add "Voldemort" as the last item on the right my_queue.popleft () --> this operation would remove "Harry", which was the second element added after "Draco", so the first element still present in the queue

VittoriaMoccia avatar Nov 25 '18 17:11 VittoriaMoccia

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

my_queue.popleft() #it removes the first element added which is Draco

my_queue.append("Voldemort") #it adds a name in this case Voldemort

my_queue.popleft() #it removes the first element added which is Harry

Output would be [“Hermione", "Ron", "Severus", Voldemort"]

lisasiurina avatar Nov 25 '18 20:11 lisasiurina

characters_queue = deque() characters_queue.append("Draco") characters_queue.append("Harry") characters_queue.append("Hermione") characters_queue.append("Ron") characters_queue.append("Severus") characters_queue.popleft() characters_queue.append("Voldemort") characters_queue.popleft() print(characters_queue) The first operation (characters_queue.popleft()) cancelled the first element added in the queue ("Draco"). The opertion characters_queue.append("Voldemort") added the string "Voldemort". The last operation (characters_queue.popleft()) cancelled the first element in the queue ("Harry").

MilenaCorbellini avatar Nov 25 '18 20:11 MilenaCorbellini

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

print (my_hp_queue)

Output ["Draco", "Harry", "Hermione", "Ron", "Severus"]

my_hp_queue.popleft() # removes the first element added my_hp_queue = deque ("Harry", "Hermione", "Ron", "Severus") my_hp_queue.append(Voldemort) # adds Voldemort to the queue my_hp_queue = deque ("Harry", "Hermione", "Ron", "Severus", "Voldemort") my_hp_queue.popleft() # removes the first element added

print (my_hp_queue)

Output [ "Hermione", "Ron", "Severus", "Voldemort"]

tceron avatar Nov 27 '18 07:11 tceron

from collections import deque my_queue = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) print(my_queue) # creates a queue containing 5 elements

my_queue.popleft() print(my_queue) # deletes the first element of the queue "Draco"

my_queue.append("Voldemort") print(my_queue) # adds a new element "Voldemord" at the end of the queue

my_queue.popleft() print(my_queue) # deletes the first element of the queue "Harry"

Here the results obtained after every 'print' stage:

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

andreamust avatar Nov 27 '18 15:11 andreamust