Problem
Given an array numbers consisting of integer elements and an integer direction representing the rotation direction, write a function solution that returns the rotated array.
- If
directionis positive, rotate the array to the right, and if negative, rotate it to the left. - For example, if
directionis-4,numbersis rotated 4 steps to the left. Ifdirectionis4,numbersis rotated 4 steps to the right.
Constraints
- Each element in
numbersis an integer with an absolute value not exceeding 500. numbersis not empty, and its length does not exceed 100.- The absolute value of
directionis at least 1 and less than the length ofnumbers.
Test Case
| Test Case Number | numbers | direction | return |
|---|---|---|---|
| 1 | [1, 2, 3, 4] | 1 | [4, 1, 2, 3] |
| 2 | [1, 2, 3, 4] | -2 | [3, 4, 1, 2] |
| 3 | [1, 2, 3, 4] | 3 | [2, 3, 4, 1] |
Explanation for Test Case 1
- Rotating
numbers1 step to the right results in[4, 1, 2, 3].
Explanation for Test Case 2
- Rotating
numbers2 steps to the left results in[3, 4, 1, 2].
Explanation for Test Case 3
- Rotating
numbers3 steps to the right results in[2, 3, 4, 1].