[코테 기초] Programmers 5문제

밍가적 사고·2024년 8월 22일

문제1.

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input.push(line);
}).on('close',function(){
   console.log(input[0]);
});

문제2.

답변

onst readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    let input = line.split(' '); 
    let a = `a = ${input[0]}`;  
    let b = `b = ${input[1]}`;  
      console.log(`${a}\n${b}`); 
})

코드 리뷰
\n 을 통해 줄 바꿈을 해준다.
백틱을 통해서 글 만들었음


문제3.

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
    let multiply = Number(input[1]);
    let text = input[0] ; 
    for(let i=0;i<multiply;i++){
      input.push(text)
    }
    input.splice(0,2)
    console.log(String(input.join("")))
}).on('close', function () {
});

코드 리뷰
split되어 있는 input 배열에서 string과 5를 꺼내
반복문에서 여러번 반복하게 만들어
다시 배열 안에 집어 넣었다
[string, 5, string, string, string, string, string]
되어 있는 배열에서
splice를 통해 0,1 번 값을 삭제한 후
조인 시켰음
slice를 쓰지 않은 이유는.. splice처럼 바로 적용되는 게 아니라
작업이 더 필요하기 때문에 쓰지 않았음

다른 사람의 코드

str = input[0];
   n = Number(input[1]);
   console.log(str.repeat(n)); 

str.repeat(n)만큼 해준다는 방법이 있었다.

for (let i = 0; i < n; i += 1) {
     str += input[0]
 }
 console.log(str)

내가 만들고 싶었던 방법은 위의 방법이었던 거 같다.
str
process.stdout.write() -> console.log()와 같이
출력하는 메서드이지만 process는 줄바꿈이 일어나지 않는다


문제 4


답변

const readline = require('readline');
const rl = readline.createInterface({
 input: process.stdin,
 output: process.stdout
});

let input = [];

rl.on('line', function (line) {
 input = [line];
}).on('close',function(){
 let str = input[0].split('');
 let newWord = []
 for(let i=0;i<str.length;i++){
     if(str[i] === str[i].toUpperCase()){
         newWord.push(str[i].toLowerCase())
     }else(
         newWord.push(str[i].toUpperCase())
     )  
 }
console.log(newWord.join(''));  
})

코드 리뷰
인덱스 넘버로 구분하기 위해서
반복문을 이용해 배열의 인덱스 번호를 하나하나 검사 돌렸다.
1. str[i] 의 상태가 str[i].toUpperCase() 한 상태와 이미 같은지 확인 시켰다.
2. 같다면 -> 소문자(toLowerCase) / 다르다면 -> 대문자 (toUpperCase)로 한 이후에 새로운 배열 newWord에 push(추가) 해주었다.
그렇게 완성한 친구들은 -> 배열 상태로 있기 때문에 join을 통해 병합하였음.

다른 사람의 코드

이 분은 forEach 와 value 값으로 대소문자를 구분하였음.

다른 분의 리뷰

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input;

rl.on('line', (line) => {
  input = [...line];
}).on('close', () => {
  console.log(
    input.map((char) => (/[a-z]/.test(char) ? char.toUpperCase() : char.toLowerCase())).join(''),
  );
});

[...@@@]형식과 /[a-z] 의 문법이 보인다.
또한 삼항연산문을 써서 풀이를 한 사람들이 꽤 된다.


문제5.


코드

줄바꿈이 일어나지 않는 process.stdout.write를 사용해보고 싶었다.
그리고 /를 실제로 출력하기 위해서는 // 이렇게 작성해야하는 것을 알았음! 오와우!

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.on('close', function () {
    let special = "\\";
    // console.log(`"!@#$%^&*(`+${special}``"<>?:;"`); 
    process.stdout.write(`!@#$%^&*(`);
    process.stdout.write(special);
    process.stdout.write(`'"<>?:;`)
});
profile
FE junior developer

0개의 댓글