ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Programmers] JavaScript 알고리즘 | Lv.0 2차원으로 만들기
    ► JS Algorithm/Programmers 2024. 8. 6. 20:03
    반응형

    🔒 문제 설명

    정수 배열 num_list와 정수 n이 매개변수로 주어집니다. num_list를 다음 설명과 같이 2차원 배열로 바꿔 return하도록 solution 함수를 완성해주세요.

    num_list [1, 2, 3, 4, 5, 6, 7, 8] 길이가 8이고 n 2이므로 num_list 2 * 4 배열로 다음과 같이 변경합니다. 2차원으로 바꿀 때에는 num_list 원소들을 앞에서부터 n개씩 나눠 2차원 배열로 변경합니다.

    num_list n result
    [1, 2, 3, 4, 5, 6, 7, 8] 2 [[1, 2], [3, 4], [5, 6], [7, 8]]

    🔒 제한사항 

    • num_list의 길이는 n의 배 수개입니다.
    • 0 ≤ num_list의 길이 ≤ 150
    • 2 ≤ n < num_list 길이

    🔒 입출력 예

    num_list n result
    [1, 2, 3, 4, 5, 6, 7, 8] 2 [[1, 2], [3, 4], [5, 6], [7, 8]]
    [100, 95, 2, 4, 5, 6, 18, 33, 948] 3 [[100, 95, 2], [4, 5, 6], [18, 33, 948]]

    🔒 입출력 예 설명

    입출력 예 #1

    • num_list가 [1, 2, 3, 4, 5, 6, 7, 8] 로 길이가 8이고 n이 2이므로 2 * 4 배열로 변경한 [[1, 2], [3, 4], [5, 6], [7, 8]] 을 return합니다.

    입출력 예 #2

    • num_list [100, 95, 2, 4, 5, 6, 18, 33, 948] 길이가 9이고 n 3이므로 3 * 3 배열로 변경한 [[100, 95, 2], [4, 5, 6], [18, 33, 948]] return합니다.

     

    🔐 solution of mine

    Array.prototype.push()

    Array.prototype.slice()

      function solution(num_list, n) {
        let start = 0;
        let last = n;
        let answer = [];
    
        while (num_list.length > last) {
          answer.push(num_list.slice(start, last));
          start += n;
          last += n;
        }
        answer.push(num_list.slice(start));
    
        return console.log(answer);
      }
    
      solution([1, 2, 3, 4, 5, 6, 7, 8], 2); //output: [[1, 2], [3, 4], [5, 6], [7, 8]]
      solution([100, 95, 2, 4, 5, 6, 18, 33, 948], 3); //output: [[100, 95, 2], [4, 5, 6], [18, 33, 948]]

     


     

    🔐 ChatGPT's refactoring

    Array.prototype.push()

    Array.prototype.slice()

      function solution(num_list, n) {
        let answer = [];
    
        for (let i = 0; i < num_list.length; i += n) {
          answer.push(num_list.slice(i, i + n));
        }
    
        return console.log(answer);
      }
    
      solution([1, 2, 3, 4, 5, 6, 7, 8], 2); //output: [[1, 2], [3, 4], [5, 6], [7, 8]]
      solution([100, 95, 2, 4, 5, 6, 18, 33, 948], 3); //output: [[100, 95, 2], [4, 5, 6], [18, 33, 948]]

     

    🔐 solution of others

    Array() 생성자

    Array.prototype.fill()

    Array.prototype.map()

    Array.prototype.splice()

      const solution = (num_list, n) =>
        console.log(
          Array(num_list.length / n)
            .fill([])
            .map((v) => num_list.splice(0, n))
        );
    
      solution([1, 2, 3, 4, 5, 6, 7, 8], 2); //output: [[1, 2], [3, 4], [5, 6], [7, 8]]
      solution([100, 95, 2, 4, 5, 6, 18, 33, 948], 3); //output: [[100, 95, 2], [4, 5, 6], [18, 33, 948]]

     

    🔐 solution of others

    Array.prototype.push()

    Array.prototype.splice()

      const solution = (num_list, n) => {
        let answer = [];
        while (num_list.length) {
          answer.push(num_list.splice(0, n));
        }
    
        console.log(answer);
      };
    
      solution([1, 2, 3, 4, 5, 6, 7, 8], 2); //output: [[1, 2], [3, 4], [5, 6], [7, 8]]
      solution([100, 95, 2, 4, 5, 6, 18, 33, 948], 3); //output: [[100, 95, 2], [4, 5, 6], [18, 33, 948]]

     

     
     
    반응형
Designed by Tistory.