From 65b889ae50d9c821b9b460dfdcba5f106429dbec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=B0=EB=A7=8C=EC=A7=84?= Date: Wed, 6 Dec 2023 16:53:32 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20problem1=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-1/problem-1.test.js | 38 ++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index c775a02..0e977db 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,6 +1,37 @@ -const solution = (numbers) => { +const solution1 = (numbers) => { + let acc = 0; + + numbers.forEach((cur) => { + acc += cur; + }); + + return acc; +}; + +const solution2 = (numbers) => { + if (numbers.length === 0) { + return 0; + } + + return numbers.pop() + solution2(numbers); }; +const solution3 = (numbers) => { + let acc = 0; + + while (true) { + if (numbers.length === 0) { + break; + } + + acc += numbers.pop(); + } + + return acc; +}; + +const solution = (numbers) => numbers.reduce((acc, cur) => acc + cur, 0); + test('빈 배열은 0을 반환한다', () => { expect(solution([])).toBe(0); }); @@ -13,6 +44,7 @@ test('배열의 합을 반환한다', () => { test('큰 배열이 입력으로 주어져도 RangeError를 던지지 않는다', () => { const input = Array.from({ length: 10000 }, (_, i) => i + 1); - expect(() => solution(input)) - .not.toThrowError(new RangeError('Maximum call stack size exceeded')); + expect(() => solution(input)).not.toThrowError( + new RangeError('Maximum call stack size exceeded'), + ); }); From db99d0dd56fc9f166d8f471a564e9f97ef8cbcc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=B0=EB=A7=8C=EC=A7=84?= Date: Thu, 7 Dec 2023 10:26:34 +0900 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20problem2=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-2/problem-2.test.js | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 458a6b8..6cf9cbe 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -1,4 +1,47 @@ +// const solutionRecursive = (n) => { +// if (n <= 0) { +// return 0; +// } + +// if (n === 1) { +// return 1; +// } + +// return solutionRecursive(n - 2) + solutionRecursive(n - 1); +// }; + +// const solutionTailRecursive = (n, prv = 0, cur = 1) => { +// if (n <= 0) { +// return prv; +// } + +// if (n === 1) { +// return cur; +// } + +// return solutionTailRecursive(n - 1, cur, prv + cur); +// }; + const solution = (n) => { + let prv = 0; + let cur = 1; + let tmp = 1; + let counter = n; + + while (true) { + if (counter <= 0) { + return prv; + } + + if (counter === 1) { + return cur; + } + + tmp = prv + cur; + prv = cur; + cur = tmp; + counter -= 1; + } }; test('음수가 주어지면 0을 반환한다', () => { From fa31cfcfba9acc15e6779a763143ae0be02ac456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=B0=EB=A7=8C=EC=A7=84?= Date: Fri, 8 Dec 2023 10:53:32 +0900 Subject: [PATCH 3/5] =?UTF-8?q?chore:=20=EC=9D=B4=EC=A0=84=20=EC=86=94?= =?UTF-8?q?=EB=A3=A8=EC=85=98=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=A3=BC?= =?UTF-8?q?=EC=84=9D=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-1/problem-1.test.js | 46 ++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index 0e977db..166fd25 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,34 +1,34 @@ -const solution1 = (numbers) => { - let acc = 0; +// const solutionEasy = (numbers) => { +// let acc = 0; - numbers.forEach((cur) => { - acc += cur; - }); +// numbers.forEach((cur) => { +// acc += cur; +// }); - return acc; -}; +// return acc; +// }; -const solution2 = (numbers) => { - if (numbers.length === 0) { - return 0; - } +// const solutionRecursive = (numbers) => { +// if (numbers.length === 0) { +// return 0; +// } - return numbers.pop() + solution2(numbers); -}; +// return numbers.pop() + solutionRecursive(numbers); +// }; -const solution3 = (numbers) => { - let acc = 0; +// const solutionLoop = (numbers) => { +// let acc = 0; - while (true) { - if (numbers.length === 0) { - break; - } +// while (true) { +// if (numbers.length === 0) { +// break; +// } - acc += numbers.pop(); - } +// acc += numbers.pop(); +// } - return acc; -}; +// return acc; +// }; const solution = (numbers) => numbers.reduce((acc, cur) => acc + cur, 0); From 7bd1f1e22c6d0cde40bbf2185d01a573c55993a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=B0=EB=A7=8C=EC=A7=84?= Date: Fri, 8 Dec 2023 10:53:57 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20problem3=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-3/problem-3.test.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index 7319eb8..39f7734 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -1,4 +1,13 @@ -const solution = (n) => { +const solution = (n, result = '') => { + if (n === 0 && result === '') { + return '0'; + } + + if (n === 0) { + return result; + } + + return solution(Math.floor(n / 2), (n % 2) + result); }; test('이진수 문자열을 반환한다', () => { From 2de2fceddfd4e49a01b99817ec066d31eb192d81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=B0=EB=A7=8C=EC=A7=84?= Date: Fri, 8 Dec 2023 11:55:22 +0900 Subject: [PATCH 5/5] =?UTF-8?q?feat:=20problem6=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-6/problem-6.test.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index 059e2b8..f411237 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -1,4 +1,37 @@ -const solution = (n) => { +// const solutionRecursive = (n) => { +// if (n === 1) { +// return 1; +// } + +// if (n === 2) { +// return 2; +// } + +// if (n === 3) { +// return 4; +// } + +// return solutionRecursive(n - 3) + solutionRecursive(n - 2) + solutionRecursive(n - 1); +// }; + +const solution = (n, memo = []) => { + if (n === 1) { + return 1; + } + + if (n === 2) { + return 2; + } + + if (n === 3) { + return 4; + } + + if (!memo[n]) { + return solution(n - 3, memo) + solution(n - 2, memo) + solution(n - 1, memo); + } + + return memo[n]; }; test('계단에 오를 수 있는 가지 수를 반환한다', () => {