Rotate List Challenge
Advanced
Let's imagine you have a list of items lined up in a row, and you want to slide them around like a game of musical chairs. Your challenge is to write a function rotate_list that moves the elements in the list a specified number of steps to the right. When an element moves past the last position, it wraps around to the beginning!
This is like spinning a tiny carousel of items in your mind!
Here's how it works:
- If you have the list
[1, 2, 3, 4, 5]and you want to rotate it 2 steps to the right, it will become[4, 5, 1, 2, 3]. - Rotating just 1 step to the right would make it
[5, 1, 2, 3, 4].
Examples
rotate_list([1, 2, 3, 4, 5], 2) # Returns: [4, 5, 1, 2, 3] rotate_list(['a', 'b', 'c', 'd'], 1) # Returns: ['d', 'a', 'b', 'c'] rotate_list(['cat', 'dog', 'mouse'], 3) # Returns: ['cat', 'dog', 'mouse'] (because 3 rotations bring it back to the original position)
Have fun rotating and keep the carousel spinning!
Inputs
Arg 1:
[1,2,3,4,5]Arg 2:
2Expected Output
[4, 5, 1, 2, 3]Actual Output