📖 Scenario: Imagine you have a list of daily tasks arranged in order. Sometimes, you want to start your day from a different task but keep the order of the remaining tasks the same. This is like rotating the list of tasks.
🎯 Goal: You will create a program that rotates an array (list) to the left by a given number of positions. This means the first few tasks move to the end, and the rest shift forward.
📋 What You'll Learn
Create a list called tasks with exact values: ['Email', 'Meeting', 'Coding', 'Lunch', 'Review']
Create a variable called rotate_by and set it to 2
Write code to rotate the tasks list to the left by rotate_by positions
Print the rotated list exactly as a Python list
💡 Why This Matters
🌍 Real World
Rotating lists is useful in scheduling, games, and data processing where order changes but elements stay the same.
💼 Career
Understanding array rotation helps in coding interviews and tasks involving data manipulation and optimization.
Progress0 / 4 steps
1
Create the initial list of tasks
Create a list called tasks with these exact values in order: 'Email', 'Meeting', 'Coding', 'Lunch', 'Review'
DSA Python
Hint
Use square brackets [] to create a list and separate items with commas.
2
Set the number of positions to rotate
Create a variable called rotate_by and set it to the number 2
DSA Python
Hint
Just assign the number 2 to the variable rotate_by.
3
Rotate the list to the left by rotate_by positions
Create a new list called rotated_tasks that contains the tasks list rotated to the left by rotate_by positions using slicing
DSA Python
Hint
Use list slicing: tasks[rotate_by:] gets the part after the first rotate_by items, and tasks[:rotate_by] gets the first rotate_by items.
4
Print the rotated list
Print the rotated_tasks list exactly as a Python list
DSA Python
Hint
Use print(rotated_tasks) to show the rotated list.
Practice
(1/5)
1. What does rotating an array to the left by 1 position do to the elements?
easy
A. Moves each element one step to the right, last element goes to the front
B. Moves each element one step to the left, first element goes to the end
C. Reverses the array
D. Sorts the array in ascending order
Solution
Step 1: Understand left rotation
Left rotation by 1 means the element at index 0 moves to the end, and all others shift left by one.
Step 2: Visualize the movement
For example, [1, 2, 3] becomes [2, 3, 1] after left rotation by 1.
Final Answer:
Moves each element one step to the left, first element goes to the end -> Option B
Quick Check:
Left rotation = shift left + wrap first element [OK]
Hint: Left rotation shifts left, first element moves to end [OK]
Common Mistakes:
Confusing left rotation with right rotation
Thinking rotation sorts the array
Assuming rotation reverses the array
2. Which of the following is the correct Python syntax to rotate an array arr to the right by 2 positions using slicing?
easy
A. arr = arr[-2:] + arr[:-2]
B. arr = arr[2:] + arr[:2]
C. arr = arr[:2] + arr[2:]
D. arr = arr[-2:] - arr[:-2]
Solution
Step 1: Understand right rotation with slicing
Right rotation by 2 means last 2 elements move to front, rest shift right.
Step 2: Apply slicing syntax
arr[-2:] gets last 2 elements, arr[:-2] gets all except last 2. Concatenating gives rotated array.
Final Answer:
arr = arr[-2:] + arr[:-2] -> Option A
Quick Check:
Right rotation by 2 = last 2 + rest [OK]
Hint: Right rotation by k = last k elements + rest [OK]
Common Mistakes:
Using + operator incorrectly with slices
Confusing left and right rotation slices
Using subtraction operator instead of concatenation