< 문제 1> 덧셈식 출력하기

두 정수 a, b 가 주어질 때 다음과 같은 형태의 계산식을 출력하는 코드를 작성해 보세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    console.log(`${Number(input[0])} + ${Number(input[1])} = ${Number(input[0])+Number(input[1])}`);
});

 

 다른 유저가 푼 방식

// 유저 1 : 구조분해
const readline = require('readline')
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
}).on('line', function (line) {
    const [a, b] = line.split(' ')
    console.log(a, '+', b, '=', Number(a) + Number(b))
})

// 유저 2 : map 과 구조분해
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let a, b;

rl.on('line', function (line) {
    [a, b] = line.split(' ').map(Number);
}).on('close', function () {
    console.log(`${a} + ${b} = ${a + b}`);
});

 

 배운 것들

     -  받은 인풋을 사전에 분리해서 받으면 좀 더 코드가 깔끔해질 수도 있다

 

<문제 2>  문자열 돌리기

문자열 str 이 주어집니다.
문자열을 시계방향으로 90도 돌려서 아래 입출력 예와 같이 출력하는 코드를 작성해 보세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
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(){
    str = input[0];
    [...str].forEach(a =>{
        console.log(a)
    })
});

 

 다른 유저가 푼 방식

// 유저 1
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(){
    str = input[0];
    for(let i = 0 ; i < str.length; i++) console.log(str[i])
});

// 유저 2 : 
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(){
    str = input[0];
    [...str].map(x=>console.log(x))
});

 

 배운 것들

     -  무

 

문제 3 홀짝 구분하기

자연수 n이 입력으로 주어졌을 때 만약 n이 짝수이면 "n is even"을,
홀수이면 "n is odd"를 출력하는 코드를 작성해 보세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    n = Number(input[0]);
    answer = n%2 === 0 ? `${n} is even` : `${n} is odd`
    return console.log(answer)
});

 

 다른 유저가 푼 방식

// 유저 1 : 요구하는 답만 얻어서 깔끔하게 처리한듯
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
}).on('line', function (line) {
    const result = Number(line) % 2 ? 'odd' : 'even'
    console.log(line, 'is', result)
})

// 유저 2 : else 대신 if(n%2 !==0) 이면 더 좋을 듯
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    n = Number(input[0]);
    if(n%2==0){
        return console.log(n+" is even")
    }
    else{
        return console.log(n+" is odd")
    }
});

 

 배운 것들

     - 점점 삼항연산자를 사용하는 것이 익숙해져 가는 것 같다.

 

 

문제 4 문자열 겹쳐쓰기

문자열 my_string, overwrite_string과 정수 s가 주어집니다.
문자열 my_string의 인덱스 s부터 overwrite_string의 길이만큼을
문자열 overwrite_string으로 바꾼 문자열을 return 하는 solution 함수를 작성해 주세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(my_string, overwrite_string, s) {
    const myLength = my_string.length
    const overLength = overwrite_string.length
    var answer = my_string.substr(0,s) + overwrite_string + my_string.substr(s+overLength, myLength);
    return answer;
}

 

 다른 유저가 푼 방식

// 유저 1 : slice 이용
unction solution(my_string, overwrite_string, s) {    
    return my_string.slice(0,s)+overwrite_string+my_string.slice(s+overwrite_string.length);
}

// 유저 2 내가 푼방식과 같은 방식이지만 시안성이 더 좋다
function solution(my_string, overwrite_string, s) {
    var answer = '';
    
    answer += my_string.substr(0,s);
    answer += overwrite_string;
    answer += my_string.substr(overwrite_string.length+s,my_string.length)

    return answer;
}

 

 배운 것들

     -  slice 와 substr 을 이용하여 주어진 문자에서 원하는 범위만큼만 선택할 수 있다.

+ Recent posts