Practice

Problem 2 of 10

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

  • steps can 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.
  • steps can 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

  1. Task 1, not checked yet. Make rotate_right pass the example tests.

  2. Task 2, not checked yet. Make rotate_right pass the hidden tests.

Task 1 of 2

Make rotate_right pass the example tests.

All tasks

main.py

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.