Skip to content

Commit 85c5640

Browse files
Add rotate_list algorithm to array category
1 parent 1ee7c81 commit 85c5640

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

array/rotate_list.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def rotate_list(lst, k):
2+
if not lst:
3+
return lst
4+
k = k % len(lst)
5+
return lst[-k:] + lst[:-k]
6+
7+
# Test the function
8+
if __name__ == '__main__':
9+
test_list = [1, 2, 3, 4, 5]
10+
print(f'Original list: {test_list}')
11+
print(f'Rotated list (k=2): {rotate_list(test_list, 2)}')
12+
print(f'Rotated list (k=3): {rotate_list(test_list, 3)}')

0 commit comments

Comments
 (0)