Skip to content

Add rotate_list algorithm #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions array/rotate_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def rotate_list(lst, k):
if not lst:
return lst
k = k % len(lst)
return lst[-k:] + lst[:-k]

# Test the function
if __name__ == '__main__':
test_list = [1, 2, 3, 4, 5]
print(f'Original list: {test_list}')
print(f'Rotated list (k=2): {rotate_list(test_list, 2)}')
print(f'Rotated list (k=3): {rotate_list(test_list, 3)}')
Comment on lines +10 to +12
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add more examples?


# Additional examples
test_list2 = ['a', 'b', 'c', 'd', 'e', 'f']
print(f'\nOriginal list: {test_list2}')
print(f'Rotated list (k=1): {rotate_list(test_list2, 1)}')
print(f'Rotated list (k=4): {rotate_list(test_list2, 4)}')

test_list3 = [10, 20, 30, 40, 50, 60, 70]
print(f'\nOriginal list: {test_list3}')
print(f'Rotated list (k=0): {rotate_list(test_list3, 0)}')
print(f'Rotated list (k=7): {rotate_list(test_list3, 7)}')
print(f'Rotated list (k=10): {rotate_list(test_list3, 10)}')
Loading