Complete the code to rotate the array to the left by one position.
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]))
We add 1 to the current index to shift elements left by one.
Complete the code to rotate the array to the right by one position.
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]))
We use -1 to move backward through the array indices.
Fix the error in the code to rotate the array left by d positions using slicing.
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))
The slice arr[:d] selects the first d elements to move to the end.
Fill both blanks to create a dictionary comprehension that maps each element to its rotated position after left rotation by 1.
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)
We add 1 to index i and use modulo to wrap around the array length.
Fill all three blanks to create a dictionary comprehension that maps uppercase elements to their rotated values after right rotation by 2.
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)
Use upper() to capitalize keys, add 2 to index, and use modulo for wrapping.