PYTH-P1

Rotate the departure board

Practice 1 of 5Python for DSA9 tests

The problem

A departure board shows a list of station codes. Every few minutes the board rotates: the first entry moves to the end. You are given the list board and a whole number k (0 or more). Return a new list that shows the board after it has rotated left k times. Do not change board itself.

rotate(["BCT", "HWH", "MAS", "SBC"], 1)
# ["HWH", "MAS", "SBC", "BCT"]
rotate(["BCT", "HWH", "MAS", "SBC"], 2)
# ["MAS", "SBC", "BCT", "HWH"]
rotate(["BCT", "HWH", "MAS", "SBC"], 6)
# the same as rotating by 2

Limits

  • Up to 1,000,000 entries. k can be bigger than the length of the board.
  • The board may be empty.

Builds on PYTH-1 Lists, indexing and slicing and PYTH-6 How practice and tests work.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

9 tests, not run yet
Test results
#TestResultReads · time
1Rotate by 1Not run
2Rotate by 2Not run
3Rotate by 0Not run
4k equals the lengthNot run
5k bigger than the lengthNot run
6Empty boardNot run
7One valueNot run
8The input is not changedNot run
9A million entries, k = 999,999Not run