[10828] ์Šคํƒ _ node.js

๋ฐ•์„œํ˜„ยท2023๋…„ 8์›” 3์ผ
0
post-thumbnail

๐ŸŒต10828 ์Šคํƒ


๐Ÿ”ป์ž…๋ ฅ๊ฐ’์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ถœ๋ ฅ์„ ํ•ด๋ณด์•˜๋”๋‹ˆ ์ถœ๋ ฅ๊ฐ’์ด ์ด์ƒํ–ˆ๋‹ค

function main () {
    // let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
    const input = require('fs').readFileSync('txt/10828.txt').toString().split('\n');
    const test = +input[0];

    for (let i = 1; i <= test; i++) {
        const command = input[i].split(' ').map(item => !isNaN(item) ? parseInt(item) : item);

        console.log(command);
    }
}

main();

๐Ÿ”ป์ถœ๋ ฅ๊ฐ’

[ 'push', 1 ]
[ 'push', 2 ]
[ 'top\r' ]
[ 'size\r' ]
[ 'empty\r' ]
[ 'pop\r' ]
[ 'pop\r' ]
[ 'pop\r' ]
[ 'size\r' ]
[ 'empty\r' ]
[ 'pop\r' ]
[ 'push', 3 ]
[ 'empty\r' ]
[ 'top' ]

\r
์บ๋ฆฌ์ง€ ๋ฆฌํ„ด์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ด์Šค์ผ€์ดํ”„ ์‹œํ€€์Šค
readFileSync ํ•จ์ˆ˜๋กœ ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๋Š” ๊ฒฝ์šฐ, Windows ์šด์˜์ฒด์ œ์—์„œ ์ค„ ๋ฐ”๊ฟˆ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ธฐํ˜ธ
\r\n (์บ๋ฆฌ์ง€ ๋ฆฌํ„ด + ๋ผ์ธ ํ”ผ๋“œ)์„ ์‚ฌ์šฉ
๐Ÿ‘‰ input ๋ฐ›์„ ๋•Œ split('\r\n')์œผ๋กœ ๋Š์–ด์ค€๋‹ค.

function main () {
    // let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
    const input = require('fs').readFileSync('txt/10828.txt').toString().split('\r\n');
    const test = +input[0];

    for (let i = 1; i <= test; i++) {
        const command = input[i].split(' ').map(item => !isNaN(item) ? parseInt(item) : item);

        console.log(command);
    }
}

main();

๐Ÿ”ป์ถœ๋ ฅ๊ฐ’

[ 'push', 1 ]
[ 'push', 2 ]
[ 'top' ]
[ 'size' ]
[ 'empty' ]
[ 'pop' ]
[ 'pop' ]
[ 'pop' ]
[ 'size' ]
[ 'empty' ]
[ 'pop' ]
[ 'push', 3 ]
[ 'empty' ]
[ 'top' ]

๋‚˜์˜ ํ’€์ด

function stack (command, num) {
    let stack = [];


    if (command === 'push') {
        const number = parseInt(num);
        stack.push(number);
        return stack;
    } else if (command === 'top') {
        return stack.length === 0? -1 : stack[stack.length-1];
    } else if (command === 'size') {
        return stack.length;
    } else if (command === 'empty') {
        return stack.length === 0 ? 1 : 0;
    } else if (command === 'pop') {
        return stack.length === 0? -1 : stack.pop();
    }
}

function main () {
    // const input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
    const input = require('fs').readFileSync('txt/10828.txt').toString().split('\r\n');
    const test = +input[0];

    for (let i = 1; i <= test; i++) {
        const [command, num] = input[i].split(' ');
        const result = stack(command, num);
        console.log(i, command, num, result);
    }

}

main();

์‹œ๊ฐ„์ดˆ๊ณผ

function main () {
    // const input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
    const input = require('fs').readFileSync('txt/10828.txt').toString().split('\r\n');
    const test = +input[0];
    let stack = [];

    for (let i = 1; i <= test; i++) {
        const [command, num] = input[i].split(' ');
        

        switch(command) {
            case 'push' :
                const number = parseInt(num);
                stack.push(number);
                break;
            case 'top' :
                console.log(stack.length === 0? -1 : stack[stack.length-1]);
                break;
            case 'size' :
                console.log(stack.length);
                break;
            case 'empty' :
                console.log(stack.length === 0 ? 1 : 0);
                break;
            case 'pop' :
                console.log(stack.length === 0? -1 : stack.pop());
                break;
        }
    }
}

main();

์‹œ๊ฐ„์ดˆ๊ณผ

โ—์‹œ๊ฐ„์ดˆ๊ณผ

console.log() ๋Š” ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค.
ํ•œ๋ฒˆ์— ๋ชจ์•„์„œ ์ถœ๋ ฅ์„ ํ•œ๋‹ค.
์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๊ณ  ๊ทธ ๋ฐฐ์—ด์— ๊ฒฐ๊ณผ๊ฐ’๋“ค์„ pushํ•œ ํ›„
๋งˆ์ง€๋ง‰์— join("\n")์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.
if๋ฌธ์„ ํ•˜๋‚˜ ํ•˜๋‚˜ ๋Œ๋ฉด ์‹œ๊ฐ„์ด ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆด๊ฒƒ๊ฐ™์•„ function์œผ๋กœ ๋งŒ๋“  ใ…Ž ํ•œ๋ฒ„๋„น ๋ชจ์•„ ์ถœ๋ ฅํ–ˆ๋‹ค.

function funcPush(stack, num) {
    const number = parseInt(num);
    stack.push(number);
}
function funcTop (stack) {
    return stack.length === 0? -1 : stack[stack.length-1];
}
function funcSize (stack) {
    return stack.length;
}
function funcEmpty (stack) {
    return stack.length === 0 ? 1 : 0;
}
function funcPop (stack) {
    return stack.length === 0? -1 : stack.pop();
}

function main () {
    // const input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
    const input = require('fs').readFileSync('txt/10828.txt').toString().split('\r\n');
    const test = +input[0];
    let stack = [];
    const result = [];

    for (let i = 1; i <= test; i++) {
        const [command, num] = input[i].split(' ');
        

        switch(command) {
            case 'push' :
                funcPush(stack, num)
                break;
            case 'top' :
                result.push(funcTop(stack))
                break;
            case 'size' :
                result.push(funcSize(stack))
                break;
            case 'empty' :
                result.push(funcEmpty(stack))
                break;
            case 'pop' :
                result.push(funcPop(stack))
                break;
        }  
    }
    console.log(result.join("\n"))
}

main();

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