프로그래밍/알고리즘 풀이
[node.js] 행복한지 슬픈지 ( 백준 10769번 )
카카수(kakasoo)
2021. 4. 11. 14:18
반응형
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", (line) => {
main(line);
rl.close();
}).on("close", () => {
process.exit();
});
const findEmotion = (line, emotion) => {
let count = 0;
for (let i = 0; i < line.length - 3; i++) {
if (line[i] === emotion[0]) {
if (line[i + 1] === emotion[1]) {
if (line[i + 2] === emotion[2]) {
count++;
}
}
}
}
return count;
};
/**
*
* @param {string} line
*/
const main = (line) => {
const happy = findEmotion(line, ":-)");
const sad = findEmotion(line, ":-(");
if (happy > sad) {
console.log("happy");
} else if (sad > happy) {
console.log("sad");
} else {
if (happy === 0) {
console.log("none");
} else {
console.log("unsure");
}
}
};
정규표현식을 이용해 풀었으면 어려웠을 테지만, 그냥도 풀 수 있는 문제라 간단했다.
반응형