Rotating a list
Write code · Medium · §3.2 Shifting and ranges
Complete the function solution, which returns a new list made by rotating the list nums to the right by k positions.
Rotating by one position moves every value one position to the right, and the last value moves to the front. Do not change the original list nums.
Function
solution(nums, k)
Constraints
- The length of
numsis from 0 to 1,000. kis an integer from 0 to 1,000,000,000.
Examples
| nums | k | result |
|---|---|---|
[1, 2, 3, 4, 5] | 2 | [4, 5, 1, 2, 3] |
[1, 2, 3] | 4 | [3, 1, 2] |
Starter code
def solution(nums, k):
answer = []
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.