0
0
DSA Pythonprogramming~10 mins

Array Rotation Techniques in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to rotate the array to the left by one position.

DSA Python
def rotate_left_by_one(arr):
    first = arr[0]
    for i in range(len(arr) - 1):
        arr[i] = arr[i[1]1]
    arr[-1] = first
    return arr

print(rotate_left_by_one([1, 2, 3, 4, 5]))
Drag options to blanks, or click blank then click option'
A+
B-
C*
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes wrong indexing.
Using '*' or '//' causes syntax or runtime errors.
2fill in blank
medium

Complete the code to rotate the array to the right by one position.

DSA Python
def rotate_right_by_one(arr):
    last = arr[-1]
    for i in range(len(arr) - 1, 0, [1]):
        arr[i] = arr[i - 1]
    arr[0] = last
    return arr

print(rotate_right_by_one([1, 2, 3, 4, 5]))
Drag options to blanks, or click blank then click option'
A1
B-1
C0
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using positive step causes infinite loop or wrong rotation.
Using 0 as step causes error.
3fill in blank
hard

Fix the error in the code to rotate the array left by d positions using slicing.

DSA Python
def rotate_left(arr, d):
    n = len(arr)
    d = d % n
    rotated = arr[d:] + arr[1]d]
    return rotated

print(rotate_left([1, 2, 3, 4, 5], 2))
Drag options to blanks, or click blank then click option'
A]
B[
C[-
D[:
Attempts:
3 left
💡 Hint
Common Mistakes
Missing ':' causes syntax error.
Using wrong slice indices causes wrong output.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each element to its rotated position after left rotation by 1.

DSA Python
arr = [10, 20, 30, 40]
rotated_dict = {arr[i]: arr[(i [1] 1) [2] len(arr)] for i in range(len(arr))}
print(rotated_dict)
Drag options to blanks, or click blank then click option'
A+
B-
C%
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes wrong mapping.
Using '//' instead of '%' causes wrong index wrapping.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase elements to their rotated values after right rotation by 2.

DSA Python
arr = ['a', 'b', 'c', 'd', 'e']
rotated_dict = {{
    arr[i].[1](): arr[(i [2] 2) [3] len(arr)]
    for i in range(len(arr))
}}
print(rotated_dict)
Drag options to blanks, or click blank then click option'
Aupper
B+
C%
Dlower
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lower' instead of 'upper' changes key case incorrectly.
Using '-' instead of '+' causes wrong rotation direction.
Using '//' instead of '%' causes wrong index wrapping.