Problem · Easy · 15 min
Rotate a list
- Easy
- Arrays
- Math
Write rotate_right(items, steps). It returns a new list in which every item moved steps places to the right. An item that moves past the end of the list comes back at the start.
For example, rotate_right([1, 2, 3, 4, 5], 2) returns [4, 5, 1, 2, 3].
stepscan be bigger than the length of the list, also very big (up to 1,000,000,000 in either direction). A rotation by the length of the list gives the same list.stepscan be negative. A negative number moves the items to the left.- An empty list gives an empty list.
Examples
| Call | Result |
|---|---|
rotate_right([1, 2, 3, 4, 5], 2) |
[4, 5, 1, 2, 3] |
rotate_right([1, 2, 3], 0) |
[1, 2, 3] |
rotate_right([], 4) |
[] |
Tasks
2 to do
Task 1, not checked yet. Make
rotate_rightpass the example tests.Task 2, not checked yet. Make
rotate_rightpass the hidden tests.
Task 1 of 2
Make rotate_right pass the example tests.