Comments (18)
'''
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"])
'''
from 2018-2019.
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"])
from 2018-2019.
my_queue = dueue(["Draco", "Harry", "Hermione", "Ron", "Severus"])
contains five elements
-
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"])
-
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"])
-
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".
from 2018-2019.
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"]
from 2018-2019.
#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'])
from 2018-2019.
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'] ) .
from 2018-2019.
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"])
from 2018-2019.
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".
from 2018-2019.
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.
from 2018-2019.
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)
from 2018-2019.
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"])
from 2018-2019.
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'])
from 2018-2019.
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”])
from 2018-2019.
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
from 2018-2019.
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"]
from 2018-2019.
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").
from 2018-2019.
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"]
from 2018-2019.
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'])
from 2018-2019.
Related Issues (20)
- Lecture "Brute-force algorithms", exercise 3 HOT 13
- Lecture "Brute-force algorithms", exercise 4 HOT 16
- Lecture "Brute-force algorithms", exercise 5 HOT 12
- Lecture "Organising information: unordered structures", exercise 1 HOT 21
- Lecture "Organising information: unordered structures", exercise 2 HOT 19
- Lecture "Organising information: unordered structures", exercise 3 HOT 17
- Lecture "Recursion", exercise 1 HOT 18
- Lecture "Recursion", exercise 2 HOT 20
- Lecture "Divide and conquer algorithm", exercise 1 HOT 12
- Lecture "Divide and conquer algorithm", exercise 2 HOT 5
- Lecture "Dynamic programming algorithms", exercise 1 HOT 15
- Lecture "Dynamic programming algorithms", exercise 2 HOT 8
- Lecture "Organising information: trees", exercise 1 HOT 4
- Lecture "Organising information: trees", exercise 2 HOT 4
- Lecture "Organising information: graphs", exercise 1 HOT 14
- Lecture "Organising information: graphs", exercise 2 HOT 12
- Christmas present game + bonus Python exercise
- Lecture "Backtracking algorithms", exercise 1 HOT 3
- Lecture "Greedy algorithms", exercise 1 HOT 2
- Question about an exercise from last year
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 2018-2019.