ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Programmers] JavaScript 알고리즘 | Lv.0 그림 확대
    ► JS Algorithm/Programmers 2024. 8. 19. 22:58
    반응형

    🔒 문제 설명

    직사각형 형태의 그림 파일이 있고, 그림 파일은 1 × 1 크기의 정사각형 크기의 픽셀로 이루어져 있습니다. 그림 파일을 나타낸 문자열 배열 picture 정수 k 매개변수로 주어질 , 그림 파일을 가로 세로로 k 늘린 그림 파일을 나타내도록 문자열 배열을 return 하는 solution 함수를 작성해 주세요.


    🔒 제한사항 

    • 1 ≤ picture의 길이 ≤ 20
    • 1 ≤ picture의 원소의 길이 ≤ 20
    • 모든 picture의 원소의 길이는 같습니다.
    • picture의 원소는 '.'과 'x'로 이루어져 있습니다.
    • 1 ≤ k ≤ 10

    🔒 입출력 예

    picture k result
    [".xx...xx.", "x..x.x..x", "x...x...x", ".x.....x.", "..x...x..", "...x.x...", "....x...."] 2 ["..xxxx......xxxx..", "..xxxx......xxxx..", "xx....xx..xx....xx", "xx....xx..xx....xx", "xx......xx......xx", "xx......xx......xx", "..xx..........xx..", "..xx..........xx..", "....xx......xx....", "....xx......xx....", "......xx..xx......", "......xx..xx......", "........xx........", "........xx........"]
    ["x.x", ".x.", "x.x"] 3 ["xxx...xxx", "xxx...xxx", "xxx...xxx", "...xxx...", "...xxx...", "...xxx...", "xxx...xxx", "xxx...xxx", "xxx...xxx"]

    🔒 입출력 예 설명

    입출력 예 #1

    • 예제 1번의 picture는 다음과 같습니다.
    .xx...xx.
    x..x.x..x
    x...x...x
    .x.....x.
    ..x...x..
    ...x.x...
    ....x....

    이를 가로 세로로 k배, 즉 2배 확대하면 그림 파일은 다음과 같습니다.

    ..xxxx......xxxx..
    ..xxxx......xxxx..
    xx....xx..xx....xx
    xx....xx..xx....xx
    xx......xx......xx
    xx......xx......xx
    ..xx..........xx..
    ..xx..........xx..
    ....xx......xx....
    ....xx......xx....
    ......xx..xx......
    ......xx..xx......
    ........xx........
    ........xx........

    따라서 ["..xxxx......xxxx..", "..xxxx......xxxx..", "xx....xx..xx....xx", "xx....xx..xx....xx", "xx......xx......xx", "xx......xx......xx", "..xx..........xx..", "..xx..........xx..", "....xx......xx....", "....xx......xx....", "......xx..xx......", "......xx..xx......", "........xx........", "........xx........"]를 return 합니다.

     

    입출력 예 #2

    • 예제 2번의 picture는 다음과 같습니다.
    x.x
    .x.
    x.x

    이를 가로 세로로 k배, 즉 3배 확대하면 그림 파일은 다음과 같습니다.

    xxx...xxx
    xxx...xxx
    xxx...xxx
    ...xxx...
    ...xxx...
    ...xxx...
    xxx...xxx
    xxx...xxx
    xxx...xxx

    따라서 ["xxx...xxx", "xxx...xxx", "xxx...xxx", "...xxx...", "...xxx...", "...xxx...", "xxx...xxx", "xxx...xxx", "xxx...xxx"] return 합니다.


     

    🔐 solution of mine

    Array.prototype.map()

    Array() 생성자

    Array.prototype.fill()

    Array.prototype.join()

    Array.prototype.flat()

    function solution(picture, k) {
      const multipleRow = picture.map((v) =>
        [...v].map((w) => Array(k).fill(w).join("")).join("")
      );
    
      return console.log(multipleRow.map((v) => Array(k).fill(v)).flat());
    }
    
    solution(
      [
        ".xx...xx.",
        "x..x.x..x",
        "x...x...x",
        ".x.....x.",
        "..x...x..",
        "...x.x...",
        "....x....",
      ],
      2
    );
    /* expected output:
    [
      "..xxxx......xxxx..",
      "..xxxx......xxxx..",
      "xx....xx..xx....xx",
      "xx....xx..xx....xx",
      "xx......xx......xx",
      "xx......xx......xx",
      "..xx..........xx..",
      "..xx..........xx..",
      "....xx......xx....",
      "....xx......xx....",
      "......xx..xx......",
      "......xx..xx......",
      "........xx........",
      "........xx........",
    ];
    */
    
    // solution(["x.x", ".x.", "x.x"], 3);
    /* expected output:
    [
      "xxx...xxx",
      "xxx...xxx",
      "xxx...xxx",
      "...xxx...",
      "...xxx...",
      "...xxx...",
      "xxx...xxx",
      "xxx...xxx",
      "xxx...xxx",
    ];
    */

     


     

    🔐 GPT's solution

    Array.prototype.forEach()

    String.prototype.split()

    Array.prototype.map()

    String.prototype.repeat()

    Array.prototype.join()

    Array.prototype.push()

    function solution(picture, k) {
      const enlargedPicture = [];
    
      picture.forEach((row) => {
        // 가로로 k배 늘리기: 각 문자를 k번 반복
        const enlargedRow = row
          .split("")
          .map((char) => char.repeat(k))
          .join("");
    
        // 세로로 k배 늘리기: 새로 만든 문자열을 k번 배열에 추가
        for (let i = 0; i < k; i++) {
          enlargedPicture.push(enlargedRow);
        }
      });
    
      return console.log(enlargedPicture);
    }
    
    solution(
      [
        ".xx...xx.",
        "x..x.x..x",
        "x...x...x",
        ".x.....x.",
        "..x...x..",
        "...x.x...",
        "....x....",
      ],
      2
    );
    /* expected output:
    [
      "..xxxx......xxxx..",
      "..xxxx......xxxx..",
      "xx....xx..xx....xx",
      "xx....xx..xx....xx",
      "xx......xx......xx",
      "xx......xx......xx",
      "..xx..........xx..",
      "..xx..........xx..",
      "....xx......xx....",
      "....xx......xx....",
      "......xx..xx......",
      "......xx..xx......",
      "........xx........",
      "........xx........",
    ];
    */
    
    // solution(["x.x", ".x.", "x.x"], 3);
    /* expected output:
    [
      "xxx...xxx",
      "xxx...xxx",
      "xxx...xxx",
      "...xxx...",
      "...xxx...",
      "...xxx...",
      "xxx...xxx",
      "xxx...xxx",
      "xxx...xxx",
    ];
    */

     

    🔐 solution of others

    Array.prototype.forEach()

    Array.prototype.reduce()

    String.prototype.repeat()

    Array.prototype.push()

    function solution(picture, k) {
      var answer = [];
    
      picture.forEach((line) => {
        const expanded = [...line].reduce(
          (acc, cur) => acc + cur.repeat(k),
          ""
        );
    
        for (let i = 0; i < k; i++) answer.push(expanded);
      });
    
      return console.log(answer);
    }
    
    solution(
      [
        ".xx...xx.",
        "x..x.x..x",
        "x...x...x",
        ".x.....x.",
        "..x...x..",
        "...x.x...",
        "....x....",
      ],
      2
    );
    /* expected output:
    [
      "..xxxx......xxxx..",
      "..xxxx......xxxx..",
      "xx....xx..xx....xx",
      "xx....xx..xx....xx",
      "xx......xx......xx",
      "xx......xx......xx",
      "..xx..........xx..",
      "..xx..........xx..",
      "....xx......xx....",
      "....xx......xx....",
      "......xx..xx......",
      "......xx..xx......",
      "........xx........",
      "........xx........",
    ];
    */
    
    // solution(["x.x", ".x.", "x.x"], 3);
    /* expected output:
    [
      "xxx...xxx",
      "xxx...xxx",
      "xxx...xxx",
      "...xxx...",
      "...xxx...",
      "...xxx...",
      "xxx...xxx",
      "xxx...xxx",
      "xxx...xxx",
    ];
    */

    🔐 solution of others

    Array() 생성자

    Array.prototype.fill()

    Array.prototype.map()

    String.prototype.replace()

    String.prototype.repeat()

    const solution = (picture, k) =>
      console.log(
        Array(picture.length * k)
          .fill(0)
          .map((v, i) => picture[~~(i / k)].replace(/./g, (v) => v.repeat(k)))
      );
    
    solution(
      [
        ".xx...xx.",
        "x..x.x..x",
        "x...x...x",
        ".x.....x.",
        "..x...x..",
        "...x.x...",
        "....x....",
      ],
      2
    );
    /* expected output:
    [
      "..xxxx......xxxx..",
      "..xxxx......xxxx..",
      "xx....xx..xx....xx",
      "xx....xx..xx....xx",
      "xx......xx......xx",
      "xx......xx......xx",
      "..xx..........xx..",
      "..xx..........xx..",
      "....xx......xx....",
      "....xx......xx....",
      "......xx..xx......",
      "......xx..xx......",
      "........xx........",
      "........xx........",
    ];
    */
    
    // solution(["x.x", ".x.", "x.x"], 3);
    /* expected output:
    [
      "xxx...xxx",
      "xxx...xxx",
      "xxx...xxx",
      "...xxx...",
      "...xxx...",
      "...xxx...",
      "xxx...xxx",
      "xxx...xxx",
      "xxx...xxx",
    ];
    */

     


     

    반응형
Designed by Tistory.