Thí dụ
def shift_list[array, s]:
"""Shifts the elements of a list to the left or right.
Args:
array - the list to shift
s - the amount to shift the list ['+': right-shift, '-': left-shift]
Returns:
shifted_array - the shifted list
"""
# calculate actual shift amount [e.g., 11 --> 1 if length of the array is 5]
s %= len[array]
# reverse the shift direction to be more intuitive
s *= -1
# shift array with list slicing
shifted_array = array[s:] + array[:s]
return shifted_array
my_array = [1, 2, 3, 4, 5]
# negative numbers
shift_list[my_array, -7]
>>> [3, 4, 5, 1, 2]
# no shift on numbers equal to the size of the array
shift_list[my_array, 5]
>>> [1, 2, 3, 4, 5]
# works on positive numbers
shift_list[my_array, 3]
>>> [3, 4, 5, 1, 2]
Tôi sử dụng cùng một ý tưởng mà bạn đã có. bật các phần tử ở bên trái và nối chúng ở bên phải. Tuy nhiên, thay vì đệ quy, tôi đã lấy tất cả các phần tử tôi cần bật cùng một lúc, bằng cách lấy
x = 32 # Shift by one position to the left res = x