Problem 115052 · medium · Level 01 Prerequisites & Setup

Rotate the Playlist

lists · slicing · modulo

A playlist keeps playing from the top. Write rotate_left(playlist, k) that returns a new list in which the first k songs have been moved to the end, in the same order. k may be larger than the length of the playlist (rotating by the length brings you back to the start). The original list must not change.

Examples

Input:  playlist = [1, 2, 3, 4, 5], k = 2
Output: [3, 4, 5, 1, 2]

Input:  playlist = [1, 2, 3], k = 3
Output: [1, 2, 3]

Constraints

  • 0 <= len(playlist) <= 5000
  • 0 <= k <= 10**6

Goals

  • Move the first k items to the end using slices
  • Handle k larger than the list length
Starting Python…