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

Examples

numskresult
[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 answer

Solve 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.