[프로그래머스LV0_JS]리스트 자르기

Hyoyoung Kim·2023년 5월 31일
0

프로그래머스 레벨0

목록 보기
26/28

리스트 자르기

https://school.programmers.co.kr/learn/courses/30/lessons/181897

문제 설명

정수 n과 정수 3개가 담긴 리스트 slicer 그리고 정수 여러 개가 담긴 리스트 num_list가 주어집니다. slicer에 담긴 정수를 차례대로 a, b, c라고 할 때, n에 따라 다음과 같이 num_list를 슬라이싱 하려고 합니다.

  • n = 1 : num_list의 0번 인덱스부터 b번 인덱스까지
  • n = 2 : num_list의 a번 인덱스부터 마지막 인덱스까지
  • n = 3 : num_list의 a번 인덱스부터 b번 인덱스까지
  • n = 4 : num_list의 a번 인덱스부터 b번 인덱스까지 c 간격으로

올바르게 슬라이싱한 리스트를 return하도록 solution 함수를 완성해주세요.

입출력 예시

코드 _ 방법 1

function solution(n, slicer, num_list) {
    var answer = [];
    switch(n){
        case 1:
            let arr1 =num_list.slice(0,slicer[1]+1);
            answer.push(...arr1);
            break;
        case 2:
            let arr2 =num_list.slice(slicer[0]);
            answer.push(...arr2);
            break;
        case 3:
            let arr3 =num_list.slice(slicer[0],slicer[1]+1);
            answer.push(...arr3);
            break;
        case 4:
            let arr4=num_list.slice(slicer[0],slicer[1]+1);
            [...arr4].map((a,i)=>i%slicer[2]===0?answer.push(a):a)
            break;
    }
    return answer;
}

console.log(solution(3,	[1, 5, 2], [1, 2, 3, 4, 5, 6, 7, 8, 9])); //[2, 3, 4, 5, 6]
console.log(solution(4,	[1, 5, 2], [1, 2, 3, 4, 5, 6, 7, 8, 9])); //[2, 4, 6]

코드 _ 방법 2

function solution(n, slicer, num_list) {
    let [a,b,c]= [...slicer]
    switch(n){
        case 1:
            return num_list.slice(0,b+1)
        case 2:
            return num_list.slice(a)
        case 3:
            return num_list.slice(a,b+1)
        case 4:
        // 1-> ture, 0-> false
            return num_list.slice(a,b+1).filter((_,idx)=>!(idx%c))
    }
}

console.log(solution(3,	[1, 5, 2], [1, 2, 3, 4, 5, 6, 7, 8, 9])); //[2, 3, 4, 5, 6]
console.log(solution(4,	[1, 5, 2], [1, 2, 3, 4, 5, 6, 7, 8, 9])); //[2, 4, 6]

0개의 댓글