프로그래머스 181891번 순서 바꾸기 - Python, Java
문제 설명
정수 리스트 num_list와 정수 n이 주어질 때, num_list를 n 번째 원소 이후의 원소들과 n 번째까지의 원소들로 나눠 n 번째 원소 이후의 원소들을 n 번째까지의 원소들 앞에 붙인 리스트를 return하도록 solution 함수를 완성해주세요.
제한 사항
- 2 ≤
num_list의 길이 ≤ 30 - 1 ≤
num_list의 원소 ≤ 9 - 1 ≤
n≤num_list의 길이
입출력 예시
| num_list | n | result |
|---|---|---|
| [2, 1, 6] | 1 | [1, 6, 2] |
| [5, 2, 1, 7, 5] | 3 | [7, 5, 5, 2, 1] |
입출력 예 설명 1
- [2, 1, 6]에서 첫 번째 이후의 원소는 [1, 6]이고 첫 번째까지의 원소는 [2]입니다. 두 리스트를 이어 붙이면 [1, 6, 2]가 됩니다.
입출력 예 설명 2
- [5, 2, 1, 7, 5]에서 세 번째 이후의 원소는 [7, 5]이고 세 번째까지의 원소는 [5, 2, 1]입니다. 두 리스트를 이어 붙이면 [7, 5, 5, 2, 1]가 됩니다.
코드 구현
Python
def solution(num_list, n):
return num_list[n:] + num_list[:n]
Java
import java.util.Arrays;
import java.util.stream.IntStream;
class Solution {
public int[] solution(int[] num_list, int n) {
int[] front = new int[num_list.length - n];
int[] back = new int[n];
for (int i = 0; i < front.length; i += 1) {
front[i] = num_list[n + i];
}
for (int i = 0; i < n; i += 1) {
back[i] = num_list[i];
}
return IntStream.concat(Arrays.stream(front), Arrays.stream(back)).toArray();
}
}