Programmers Coding Challenge 1 : ๋ณ„์ฐ๊ธฐ

Minhyeok Kimยท2022๋…„ 8์›” 3์ผ
0

์•Œ๊ณ ๋ฆฌ์ฆ˜(Algorithm)

๋ชฉ๋ก ๋ณด๊ธฐ
1/5

I was shocked at the moment I saw the question. Because i had never seen the given codes before๐Ÿฅฒ. Although i have just started studying IT things, I could not imagine that I was unable to understand what they want me to do with the given codes from the level 1 question๐Ÿคฃ.

Given description,
์ด ๋ฌธ์ œ์—๋Š” ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ n๊ณผ m์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
(๋ณ„ : * ) ๋ฌธ์ž๋ฅผ ์ด์šฉํ•ด ๊ฐ€๋กœ์˜ ๊ธธ์ด๊ฐ€ n, ์„ธ๋กœ์˜ ๊ธธ์ด๊ฐ€ m์ธ ์ง์‚ฌ๊ฐํ˜• ํ˜•ํƒœ๋ฅผ ์ถœ๋ ฅํ•ด๋ณด์„ธ์š”.

Given condition,
์ œํ•œ ์กฐ๊ฑด
n๊ณผ m์€ ๊ฐ๊ฐ 1000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

// Given code
process.stdin.setEncoding('utf8');
process.stdin.on('data', data => {
    const n = data.split(" ");
    const a = Number(n[0]), b = Number(n[1]);
    console.log(a);
    console.log(b);
});

So I had to figure out what "process", "stdin" and "setEncoding" mean.

process.stdin.setEncoding('utf8');
process.stdin.on('data', data => //
// it looks excuting the code : standard input & output based on my research!
// once the code gets 'data', it goes through the function on the next
// it is separated in to two numbers. 
    const n = data.split(" ");
    const a = Number(n[0]), b = Number(n[1]);
// the first letter (index 0) goes to variable 'a'
// the second letter (index 1) goes to variable 'b'
// and, it excutes rest of codes.

// ์ž…๋ ซ๋ฐ›์€ ์ •๋ณด 'data' ๋ฅผ n ์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ๋ฐ•๊ณ  split, -> ๋ฐฐ์—ดํ™”
// n ๋ฐฐ์—ด์—์„œ ์ธ๋ฑ์Šค 0์— ์žˆ๋Š” ์ •๋ณด๋ฅผ ์ˆซ์ž๋กœ ๋งŒ๋“ค๊ณ  ๋ณ€์ˆ˜ a ์— ํ• ๋‹น
// ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋ฐฐ์—ด ์ธ๋ฑ์Šค 1์— ์žˆ๋Š” ์ •๋ณด๋ฅผ ๋ณ€์ˆ˜ b ์—ํ• ๋‹น
// ํ™œ์šฉ์ค€๋น„ ๋

process.stdin.setEncoding("utf8");
process.stdin.on("data", (data) => {
  const n = data.split(" ");
  const a = Number(n[0]),
    b = Number(n[1]);
  let star = function (a, b) {
    for (let i = 0; i < m; i++) {
      if (i < m) {
        for (let k = 0; k < n; k++) {
          console.log("*");
        }
      }
    }
  };
});

์™œ ์ถœ๋ ฅ์ด ... ์•ˆ๋˜์—ˆ๋Š”๊ฐ€...

๊ตณ์ด ํ•จ์ˆ˜๋กœ ์ •์˜ํ•ด์„œ ํ• ํ•„์š”๊ฐ€ ์—†์—ˆ๊ณ  ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœ์‹์ด ๋”ฐ๋กœ ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ ์•„๋งˆ ๊ทธ๋Ÿฐ๊ฑด ์•ˆ๋˜์ ธ์ฃผ๋Š”๋“ฏ

process.stdin.setEncoding("utf8");
process.stdin.on("data", (data) => {
  const n = data.split(" ");
  const a = Number(n[0]),
    b = Number(n[1]);

  for (let i = 0; i < b; i++) {
    star = "";
    for (let k = 0; k < a; k++) {
      star += "*";
    }
    console.log(star);
  }
});

์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ ํ†ต๊ณผ ๋˜์—ˆ๋‹ค... ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๊ฐ€ ์–ด๋–ป๊ฒŒ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š”์ง€ ์ดํ•ด๋ฅผ ํ•ด์•ผํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๋“ฏ
ํ˜ผ์ž ํ• ๋•Œ ์‚ฌ์šฉํ•˜๋˜ ๋ฐฉ๋ฒ•์€ ๋ง ๊ทธ๋Œ€๋กœ ๋‚ด๊ฐ€ ๋‹ค ํ†ต์ œํ•˜๋Š”๋ฐ ์ฝ”๋”ฉ์ด๋ผ๋Š”๊ฒŒ ๊ฒฐ๊ตญ ์ƒํ˜ธ์ž‘์šฉ์„ ์œ„ํ•œ ์–ธ์–ด์ด๋‹ˆ๊นŒ

๋‹ค๋ฅธ์‚ฌ๋žŒ๋“ค์€ ์–ด๋–ป๊ฒŒ?

How do other people do ?

process.stdin.setEncoding("utf8");
process.stdin.on("data", (data) => {
  const n = data.split(" ");
  const a = Number(n[0]),
    b = Number(n[1]);
  const row = "*".repeat(a);
  for (let i = 0; i < b; i++) {
    console.log(row);
  }
});
// they used "repeat" to make the rows
// ๋ณ„์„ a๋ฒˆ ์ฐ๋Š” ์ž‘์—…์„ "row"์— ํ• ๋‹นํ•ด์„œ
// column ์„ b ๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ ๋งŒ๋“ค๋•Œ๋งˆ๋‹ค "row"๋ณ€์ˆ˜๋ฅผ ํ˜ธ์ถœ
// console.log ์—๋Š” ์ž๋™์œผ๋กœ ์ค„๋ฐ”๊ฟˆํ•˜๋Š”๊ฒŒ ์žˆ์–ด์„œ
// ์ค„๋ฐ”๊ฟˆ์ด ๋”ฐ๋กœ ํ•„์š”ํ•˜์ง„ ์•Š์Œ

0๊ฐœ์˜ ๋Œ“๊ธ€