← Back to Data Structures & Algorithms
Question 538
Rotate Left By Slicing
Rotate Left By Slicing
Write rotate_left(values: list[int], k: int) -> list[int] that returns a new list with the first k items moved to the end, so rotating 1 2 3 4 5 by 2 gives 3 4 5 1 2. Use two slices joined with +, and first reduce k with the remainder operator % by the length so that a k larger than the list wraps around; an empty list is returned as is. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: two lines, a space-separated list of integers (possibly empty) and then k; the tests print the list your function returns.
Expected Output:
[3, 4, 5, 1, 2]= newline. The output ends with a newline — print one after the last line.
Hints2 available
Sign in to use hintsTopics:
Data Structures & AlgorithmsSlicing
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.