Code Explanation:
1. from collections import deque
Explanation:
Imports the deque class from the collections module. A deque (double-ended queue) allows fast appends and pops from both ends.
Output:
No output.
2. d = deque()
Explanation:
Creates an empty deque named d.
Current state of d:
deque([])
Output:
No output.
3. d.appendleft(1)
Explanation:
Adds 1 to the left end of the deque.
Current state of d:
deque([1])
Output:
No output.
4. d.appendleft(2)
Explanation:
Adds 2 to the left end. Since 1 is already there, 2 goes before it.
Current state of d:
deque([2, 1])
Output:
No output.
5. d.append(3)
Explanation:
Adds 3 to the right end (normal append).
Current state of d:
deque([2, 1, 3])
Output:
No output.
6. d.rotate(1)
Explanation:
Rotates the deque right by 1 position.
This means the last element moves to the front.
Before rotation:
deque([2, 1, 3])
After rotation:
deque([3, 2, 1])
7. print(list(d))
Explanation:
Converts the deque to a list and prints it.
Output:
[3, 2, 1]
.png)

0 Comments:
Post a Comment