Python: Rotate a Deque Object specified number (positive) of times
Write a Python program to rotate a Deque Object a specified number (positive) of times.
Sample Solution:
Python Code:
# Import the collections module to use the deque data structure
import collections
# Declare an empty deque object named 'dq_object'
dq_object = collections.deque()
# Add elements to the deque from left to right
dq_object.append(2)
dq_object.append(4)
dq_object.append(6)
dq_object.append(8)
dq_object.append(10)
# Print a message to indicate the display of the deque before rotation
print("Deque before rotation:")
# Print the content of 'dq_object'
print(dq_object)
# Rotate the deque once in the positive direction (to the right)
dq_object.rotate()
# Print a message to indicate the display of the deque after 1 positive rotation
print("\nDeque after 1 positive rotation:")
# Print the content of 'dq_object' after one rotation
print(dq_object)
# Rotate the deque twice in the positive direction (to the right)
dq_object.rotate(2)
# Print a message to indicate the display of the deque after 2 positive rotations
print("\nDeque after 2 positive rotations:")
# Print the content of 'dq_object' after two rotations
print(dq_object)
Sample Output:
Deque before rotation: deque([2, 4, 6, 8, 10]) Deque after 1 positive rotation: deque([10, 2, 4, 6, 8]) Deque after 2 positive rotations: deque([6, 8, 10, 2, 4])
Flowchart:
Python Code Editor:
Previous: Write a Python program to count the number of times a specific element presents in a deque object.
Next: Write a Python program to rotate a deque Object specified number (negative) of times.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics