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.
kcan 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
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
9 tests, not run yet
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | Rotate by 1 | Not run | |
| 2 | Rotate by 2 | Not run | |
| 3 | Rotate by 0 | Not run | |
| 4 | k equals the length | Not run | |
| 5 | k bigger than the length | Not run | |
| 6 | Empty board | Not run | |
| 7 | One value | Not run | |
| 8 | The input is not changed | Not run | |
| 9 | A million entries, k = 999,999 | Not run |