memostack
article thumbnail
백준 10951번 / A+B - 4 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 6. 15:25

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 단순히 입력 받아서 두 수를 더한 값을 출력해주면 되는 간단한 문제이다. const solution = (input) => input .map((l) => { const [a, b] = l.split(' ').map((s) => +s); return a + b; }) .join('\n'); const input = []; require('readline') .createInterface({ input: process.stdin }) .on('line', (line) => input.push(line)) .on('close', (_) => { console.log(solution(input)); process..

article thumbnail
백준 10952번 / A+B - 5 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 5. 20:41

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 const solution = (input) => { let findZero = false; const nums = input.reduce((acc, l) => { if (findZero) return acc; if (l === '0 0') { findZero = true; } else { acc.push(l.split(' ').map((s) => +s)); } return acc; }, []); return nums .slice(0, nums.length) .map((n) => n[0] + n[1]) .join('\n'); }; const input = []; require('readline') .crea..

article thumbnail
백준 10871번 / X보다 작은 수 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 4. 21:41

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 첫째 줄과 두번째 줄을 입력받고 미리 정수형으로 파싱하고 문제를 풀었다. 리스트의 filter 함수를 이용하여, X보다 작은 수를 필터링했다. const solution = (input) => { const [l1, l2] = input.map((l) => l.split(' ').map((s) => +s)); return l2 .slice(0, l1[0] + 1) .filter((n) => n < l1[1]) .join(' '); }; const input = []; require('readline') .createInterface({ input: process.stdin }) .on('line', (lin..

article thumbnail
백준 2439번 / 별 찍기 - 2 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 3. 21:40

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 앞에 여백과 * 를 합쳐주면 되는 간단한 문제이다. repeat() 함수를 이용하면 좀 더 깔끔하게 풀 수 있다. const solution = (input) => [...Array(+input).keys()] .reduce((acc, n) => { acc.push([' '.repeat(+input - n - 1), '*'.repeat(n + 1)].join('')); return acc; }, []) .join('\n'); const print = (input) => console.log(solution(input + '')); process.stdin.on('data', print); 전체 코드 http..

article thumbnail
백준 2438번 / 별 찍기 - 1 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 2. 21:11

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 문자열의 repeat() 함수를 이용하면, for문을 돌리면서 문자열을 계속 더해줄 필요가 없다. const solution = (input) => [...Array(+input).keys()] .reduce((acc, n) => { acc.push('*'.repeat(n + 1)); return acc; }, []) .join('\n'); const print = (input) => console.log(solution(input + '')); process.stdin.on('data', print); 전체 코드 https://github.com/bluemiv/Algorithm/blob/master/bae..

article thumbnail
백준 11022번 / A+B - 8 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 1. 20:49

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 입력 받은 두 값을 더한 뒤에 Case#1: 1 + 1 = 2 형태에 맞게 출력해주면 되는 간단한 문제이다. 자주 사용하는 문법중에 문자열을 정수로 변환할때 parseInt()를 사용해도 되지만, 주로 + 기호를 이용하여 문자열을 정수로 바꿔준다. const solution = (input) => input .slice(1, input[0] + 1) .map((s, idx) => { const [a, b] = s.split(' '); return `Case #${idx + 1}: ${a} + ${b} = ${+a + +b}`; }) .join('\n'); const input = []; require('re..

article thumbnail
백준 11021번 / A+B - 7 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 31. 14:39

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 입력 받은 두 값을 더한 뒤에 Case#1: 2 형태에 맞게 출력해주면 되는 간단한 문제이다. 문제를 깔끔하게 풀기위해 reduce를 이용하여 합을 구하고 문자 템플릿`Case #${idx + 1}: ${n}`을 사용했다 const solution = (input) => input .slice(1, input[0] + 1) .map((s) => s.split(' ').reduce((acc, n) => acc + +n, 0)) .map((n, idx) => `Case #${idx + 1}: ${n}`) .join('\n'); const input = []; require('readline') .createIn..

article thumbnail
백준 2742번 / 기찍 N (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 30. 20:31

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 아래 문제와 역순으로 입력값부터 1까지 개행하여 출력하면 된다. 2022.07.29 - [Algorithm/Beakjoon] - 백준 2741번 / N 찍기 (nodejs 알고리즘 풀이) const solution = (input) => [...Array(+input).keys()].map((i) => +input - i).join('\n'); const print = (input) => console.log(solution(input + '')); process.stdin.on('data', print); 전체 코드 https://github.com/bluemiv/Algorithm/blob/master/b..

article thumbnail
백준 2741번 / N 찍기 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 29. 21:26

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다. 문제 풀이 1부터 입력값까지 개행하여 출력주면 되는 간단한 문제이다. const solution = (input) => [...Array(+input).keys()].map((i) => i + 1).join('\n'); const print = (input) => console.log(solution(input + '')); process.stdin.on('data', print); 전체 코드 https://github.com/bluemiv/Algorithm/blob/master/baekjoon/nodejs/src/ex02/ex2741.js GitHub - bluemiv/Algorithm: 알고리즘 풀이 알고리즘 풀..

article thumbnail
백준 15552번 / 빠른 A+B (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 28. 21:17

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다. 문제 풀이 javascript로 풀때는 콘솔 입력을 받기 위해, 딱히 신경 쓸 필요가 없는 듯 하다. 아래 문제와 같이 단순히 입력 받아서 더한 값을 출력해주면 된다. 2022.07.26 - [Algorithm/Beakjoon] - 백준 10950번 / A+B - 3 (nodejs 알고리즘 풀이) const solution = (input) => { return input .slice(1, input[0] + 1) .map((l) => l.split(' ').reduce((acc, n) => acc + +n, 0)) .join('\n'); }; const input = []; require('readline') .c..

article thumbnail
백준 8393번 / 합 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 27. 21:05

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다. 문제 풀이 단순히 1 부터 입력받은 값까지 더하는 문제로 한번의 loop를 통해 문제를 풀 수 있다 const solution = (input) => { return [...Array(+input).keys()].reduce((acc, i) => acc + (i + 1), 0); }; const print = (input) => console.log(solution(input + '')); process.stdin.on('data', print); 전체 코드 https://github.com/bluemiv/Algorithm/blob/master/baekjoon/nodejs/src/ex08/ex8393.js GitHu..

article thumbnail
Rollup 모듈에서 Cannot read properties of null (reading 'useState') 오류 조치
Frontend/React 2022. 7. 26. 21:32

오류 내용 rollup으로 공통으로 사용할 ui 모듈을 생성하여, web app에 가져다가 사용하는 도중에 아래와 같은 오류가 발생했다. Uncaught TypeError: Cannot read properties of null (reading 'useState') 오류 내용은 null에서 useState를 읽으려고 하다보니 발생한다. 공통 모듈 개발 당시에는 문제없이 동작을 했는데, rollup 번들링을 하고 나면 오류가 발생한다. 공통 모듈 개발 당시 정상 동작 확인 오류 조치 rollup 빌드시 external 옵션에 react, react-dom을 추가해줘야 한다. (만약 styled-components를 사용한다면, 같이 포함해줘야 함) rollup.config.js // ... export d..

article thumbnail
백준 10950번 / A+B - 3 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 26. 20:58

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다. 문제 풀이 처음에는 process.stdin.on()으로 입력을 받아서 풀었는데, 여러 줄을 입력을 받을 수 가 없어서 readline 모듈을 활용하여 여러 줄을 받을 수 있도록 했다. const solution = (input) => { const lines = input.map((s) => s.trim()); return lines .slice(1, lines[0] + 1) .map((l) => l.split(' ').reduce((acc, _n) => acc + +_n, 0)) .join('\n'); }; const input = []; require('readline') .createInterface({ in..

article thumbnail
백준 2739번 / 구구단 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 25. 21:04

문제 풀이 단순히 반복해서 숫자를 곱해주면 되는 문제라 프로그래밍을 처음 접하는 사람이 풀어보기 좋은 문제다 const solution = (input) => { const num = +input; return [...Array(9).keys()].map((idx) => `${num} * ${idx + 1} = ${num * (idx + 1)}`).join('\n'); }; const print = (input) => console.log(solution(input + '')); process.stdin.on('data', print); 전체 코드 https://github.com/bluemiv/Algorithm/blob/master/baekjoon/nodejs/src/ex02/ex2739.js GitH..

article thumbnail
백준 2480번 / 주사위 세개 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 24. 16:33

문제 문제 풀이 input 값들을 map 자료구조에 담아서, 동일한 주사위 눈의 개수가 몇개인지 파악하도록 했다. 만약 map의 key 값이 1개인 경우, 주사위 3개의 눈이 모두 동일한 경우 (e.g. 6, 6, 6) 2개인 경우, 주사위 눈 중 2개가 동일하고 1개가 다른 경우 (e.g. 2, 2, 5) 그 외에는 주사위 3개가 모두 눈이 다른 경우 (e.g. 2, 3, 6) const solution = (input) => { const ns = input.split(' ').map((s) => +s); const nm = ns.reduce((acc, n) => { if (acc[n]) { return { ...acc, [n]: acc[n] + 1 }; } else { return { ...acc..

article thumbnail
백준 2525번 / 오븐 시계 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 23. 15:47

문제 문제 풀이 문제를 요약하면, 현재 시간에서 입력받은 분(minute)을 더한 뒤의 시각을 구하면 된다. const solution = (input) => { const [a, b, c] = input.split(/\s+/).map((s) => +s); const sum = b + c; const carry = parseInt(sum / 60); const m = sum % 60; const h = (carry > 0 ? a + carry : a) % 24; return `${h} ${m}`; }; const print = (input) => console.log(solution(input + '')); process.stdin.on('data', print); 분은 60으로 나머지 연산을 하면 구할..

article thumbnail
백준 2884번 / 알람 시계 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 22. 20:28

문제 문제 풀이 문제를 요약하면, 현재 시간에서 45분을 뺀 시각을 구하면 된다. 시간 구하기 M이 45분보다 작은 경우, 시간이 바뀌기 때문에 -1을 해준다. 단 H의 값이 0 인경우에는 -1을 하면 23이 된다 분 구하기 M이 45분보다 작다면 60 + M - 45를 해주고, 크거나 같다면 M - 45를 해준다 const solution = (input) => { const [h, m] = input.split(' ').map((s) => +s); const nextH = m { const input..

article thumbnail
jest 단위테스트를 이용하여 백준 알고리즘 문제 편하게 풀기
Algorithm/Beakjoon 2022. 7. 21. 20:48

쉬운 문제는 웹 사이트에서 풀어도 되지만, 복잡한 문제는 여러 경우의 케이스에 대해서 실행해보는 경우가 생긴다. 백준에서는 다른 알고리즘 문제 사이트와 다르게 웹에서 실행해볼수가 없어서 로컬에서 따로 실행시켜봐야하는 불편함이 있다. 로컬에서 실행할때 항상 node [파일명].js 명령어로 수행해보면서 문제를 풀었는데, 이방법도 상당히 귀찮아서 jest를 이용하여 편하게 문제를 풀어보려고 한다. jest 설정 프로젝트 설정 우선 단위테스트를 하기 위해, jest를 설치한다. yarn init -y yarn add --dev jest jest config 설정 설치 후 jest.config.js 파일을 작성한다. jest는 설정 내용을 package.json에 작성하거나, jest.config.js에 작성할..

article thumbnail
백준 10869번 / 사칙연산 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 20. 20:40

문제 풀이 2개의 숫자를 입력받아, 사칙연산 값과 나머지 연산 값을 출력하면 되는 문제이다. 주의할 점은 나눗셈을 한 뒤에 몫만 출력해야하기 때문에 정수값으로 파싱해주는 작업을 해야한다. (솔직히 한번 틀림) const solution = (input) => { const [a, b] = (input + '').split(' ').map(s => +s); console.log(a + b); console.log(a - b); console.log(a * b); console.log(parseInt(a / b)); console.log(a % b); } process.stdin.on('data', solution); 전체 코드 https://github.com/bluemiv/Algorithm/blob/ma..

article thumbnail
백준 1000번 / A+B (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 7. 19. 20:17

직장 생활하다보니 알고리즘 문제를 안풀어본지 오래된거 같아서, 다시 알고리즘 문제를 풀어보기로 했다. 매일 풀면 좋겠지만 시간이 가능하려나.. 문제 풀이 nodejs에서는 콘솔 입력을 받을때 process.stdin.on을 사용한다. 백준에서 콘솔 입력을 받는 여러가지 방법이 있는것으로 보이나, 아래 방법이 제일 간단해보여서 아래와 같이 풀었다. 문제는 상당히 쉬운 문제로, 단순히 2개의 숫자를 입력받아서 더한 값을 콘솔 출력하는 문제이다. (프로그래밍 입문자가 문법을 익힐겸 풀기엔 좋은것 같음) const solution = (input) => { const [a, b] = (input + '').split(' ').map(s => +s); console.log(a + b); } process.stdi..