문제

문자열 str1, str2가 매개변수로 주어집니다. str1 안에 str2가 있다면 1을 없다면 2를 return하도록 solution 함수를 완성해주세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(str1, str2) {
    var answer = str1.indexOf(str2)=== -1 ? 2 : 1
    return answer;
}

 

 다른 유저가 푼 방식

// 유저 1 inclue
function solution(str1, str2) {
    return str1.includes(str2) ? 1 : 2;
}

// 유저 2 split : 없으면 나누어지지 않으니까 이 컨셉을 이용
function solution(str1, str2) {
    return str1.split(str2).length > 1 ? 1 : 2
}

// 유저 3

 

 배운 것들

     - split 을 이용하여 문자에서 특정 문자가 존재하는지를 파악하는 방법

     - string 에 include 가 존재한다.

< 문제 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 을 이용하여 주어진 문자에서 원하는 범위만큼만 선택할 수 있다.

문제 1

어떤 자연수를 제곱했을 때 나오는 정수를 제곱수라고 합니다. 정수  n 이 매개변수로 주어질 때, 
n 이 제곱수라면 1을 아니라면 2를 return하도록 solution 함수를 완성해주세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(n) {
    var answer = 2;
    for(let i = 0; i <= n; i++){
        if (i*i === n){
            answer = 1
            break;
        }
    }
    return answer;
}

 

 다른 유저가 푼 방식

// 유저 1 : 루트를 이용하여 제곱수인지 파악
function solution(n) {
  return Number.isInteger(Math.sqrt(n)) ? 1 : 2;
}

// 유저 2 : 주어진 수의 절반에 가까운 것만 확인하기
function solution(n) {
    for (let i = 0; i <= n/2; i++) {
        if (i*i == n) {
            return 1;
        }
    }
    return 2;
}

 

 배운 것들

     -  수학적인 논리로 쉽게 해결할 수 있는 문제였다.

문제 1

문자열  str 이 주어질 때,  str 을 출력하는 코드를 작성해 보세요.

 

▶ 내가 푼 방식

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

let input = [];

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

 

 다른 유저가 푼 방식

// 유저 1
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
}).on('line', console.log)

 

문제 2

주어진 문자열 과 정수를 이용하여 정수만큼 문자열을 반복하여 출력하세요

 

▶ 내가 푼 방식

//내가 작성한 코드
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 () {
    str = input[0];
    n = Number(input[1]);
    result = [];
    for(let i =0; i<n; i++){
        result.push(str)
    }
    console.log(result.join(''))
});

 

 다른 유저가 푼 방식

// 유저 1 : repeat
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 () {
    str = input[0];
    n = Number(input[1]);
    console.log(str.repeat(n));
});

// 유저 2 : process.stdout.write
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 () {
    str = input[0];
    n = Number(input[1]);
    for (i=0; i<n; i++){
        process.stdout.write(str);   
    }

});


// 유저 3

 

 배운 것들

     - repeat 함수로 반복 가능

     - process.stdout.write() 도 가능 

 

문제 3 대문자소문자 바꿔서 출력하기

영어 알파벳으로 이루어진 문자열  str 이 주어집니다.
각 알파벳을 대문자는 소문자로 소문자는 대문자로 변환해서 출력하는 코드를 작성해 보세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
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 ++){
        const word = /[A-Z]/.test(str[i]) ? str[i].toLowerCase() : str[i].toUpperCase();
        process.stdout.write(word)
    }
});

 

 다른 유저가 푼 방식

// 유저 1
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(''),
  );
});

//유저 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];
    const regex = /[A-Z]/
    console.log([...str].map((v)=> regex.test(v) ? v.toLowerCase() : v.toUpperCase()).join(''))
});

 

 배운 것

     -  스프레드 방식으로 문자를 배열로 만들수 있다는 점

 

문제 4

!@#$%^&*(\'"<>?:; 출력하기

 

▶ 내가 푼 방식

//내가 작성한 코드
// !@#$%^&*(\'"<>?:; 을 출력하기 위해서 \ 추가
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input;
rl.on('close', function () {
    const output = `!@#$%^&*(\\'"<>?:;`
    process.stdout.write(output)
    
});

 

문제 1

머쓱이는 할머니께 생신 축하 편지를 쓰려고 합니다. 할머니가 보시기 편하도록 글자 한 자 한 자를 가로 2cm 크기로 적으려고 하며, 편지를 가로로만 적을 때, 축하 문구 message 를 적기 위해 필요한 편지지의 최소 가로길이를 return 하도록 solution 함수를 완성해주세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(message) {
    return message.length*2;
}

 

 다른 유저가 푼 방식

// 유저 1
function solution(message) {
    var horizontal = null;
    if((typeof message)=="string" ){
        horizontal = message.length * 2;
    }

    return horizontal;
}

 

 배운 것들

     -  무

+ Recent posts