From f9c51762eb8a146b3c1700df6e96ff65137ed651 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sun, 10 Sep 2023 15:06:30 +0900 Subject: [PATCH 1/8] =?UTF-8?q?feat:=20problem-1=20=EC=B2=AB=20=EB=B2=88?= =?UTF-8?q?=EC=A7=B8=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-1/problem-1.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index c775a02..7535bcd 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,4 +1,9 @@ const solution = (numbers) => { + if (!numbers.length) { + return 0; + } + + return numbers.reduce((acc, cur) => acc + cur, 0); }; test('빈 배열은 0을 반환한다', () => { From 25beac5a4f85bfc2bc36ec4fc09ad12f8e545e61 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sun, 10 Sep 2023 15:07:00 +0900 Subject: [PATCH 2/8] =?UTF-8?q?feat:=20problem-2=20=EC=B2=AB=20=EB=B2=88?= =?UTF-8?q?=EC=A7=B8=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-2/problem-2.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 458a6b8..eb752d5 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -1,4 +1,15 @@ const solution = (n) => { + if (n < 0) { + return 0; + } + + const result = [0, 1]; + + for (let i = 2; i <= n; i += 1) { + result[i] = result[i - 1] + result[i - 2]; + } + + return result[n]; }; test('음수가 주어지면 0을 반환한다', () => { From d0074b497853f34d48cdb71a49ac828f1916ca96 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sun, 10 Sep 2023 15:07:06 +0900 Subject: [PATCH 3/8] =?UTF-8?q?feat:=20problem-3=20=EC=B2=AB=20=EB=B2=88?= =?UTF-8?q?=EC=A7=B8=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-3/problem-3.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index 7319eb8..3645a7d 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -1,4 +1,19 @@ const solution = (n) => { + if (n < 2) { + return n.toString(); + } + + let current = n; + let result = ''; + + while (current > 0) { + const remains = current % 2; + + result = remains.toString() + result; + current = Math.floor(current / 2); + } + + return result; }; test('이진수 문자열을 반환한다', () => { From a8f559b1e98a681a7802f77e80eefd6617d4a995 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sun, 10 Sep 2023 15:07:09 +0900 Subject: [PATCH 4/8] =?UTF-8?q?feat:=20problem-4=20=EC=B2=AB=20=EB=B2=88?= =?UTF-8?q?=EC=A7=B8=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-4/problem-4.test.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 19f5cb0..bfe7f91 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,4 +1,13 @@ -const solution = () => { +const solution = (input = '') => { + let result = 0; + let number = 0; + + for (let index = input.length - 1; index >= 0; index -= 1) { + result += parseInt(input.charAt(index), 10) * (2 ** number); + number += 1; + } + + return result; }; test('10진수 숫자를 반환한다', () => { From dca1686d5ded3707c7e8744bc4bacaa36ac7b5fe Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sun, 10 Sep 2023 15:07:12 +0900 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20problem-5=20=EC=B2=AB=20=EB=B2=88?= =?UTF-8?q?=EC=A7=B8=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-5/problem-5.test.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/problem-5/problem-5.test.js b/problem-5/problem-5.test.js index 20a8fab..a3035f7 100644 --- a/problem-5/problem-5.test.js +++ b/problem-5/problem-5.test.js @@ -1,4 +1,11 @@ -const solution = () => { +const solution = (a, b) => { + const r = a % b; + + if (r === 0) { + return b; + } + + return solution(b, r); }; test('최대 공약수를 반환한다', () => { From 8c979901c1a6c00323ee59de70f58266e2e096dd Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sun, 10 Sep 2023 15:07:17 +0900 Subject: [PATCH 6/8] =?UTF-8?q?feat:=20problem-6=20=EC=B2=AB=20=EB=B2=88?= =?UTF-8?q?=EC=A7=B8=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-6/problem-6.test.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index 059e2b8..f24ccf7 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -1,4 +1,26 @@ const solution = (n) => { + const memo = {}; + + // 메모이제이션 전역 변수를 사용하지 않기 위해서 helper 함수 선언 + const helper = (num) => { + if (num < 0) { + return 0; + } + + if (num === 0) { + return 1; + } + + if (memo[num]) { + return memo[num]; + } + + memo[num] = helper(num - 3) + helper(num - 2) + helper(num - 1); + + return memo[num]; + }; + + return helper(n); }; test('계단에 오를 수 있는 가지 수를 반환한다', () => { From fb346367fd48f831eb2b8498b607e82b429c32c6 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Wed, 13 Sep 2023 23:38:11 +0900 Subject: [PATCH 7/8] =?UTF-8?q?refactor:=20problem-1=20=EB=B6=88=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20if=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-1/problem-1.test.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index 7535bcd..6def013 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,10 +1,4 @@ -const solution = (numbers) => { - if (!numbers.length) { - return 0; - } - - return numbers.reduce((acc, cur) => acc + cur, 0); -}; +const solution = (numbers) => numbers.reduce((acc, cur) => acc + cur, 0); test('빈 배열은 0을 반환한다', () => { expect(solution([])).toBe(0); From 48cc02a78a5cee3fc7778ba9c8eaa0d02b33b194 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Wed, 13 Sep 2023 23:45:37 +0900 Subject: [PATCH 8/8] =?UTF-8?q?fix:=20problem-4=20=EC=9E=85=EB=A0=A5=20?= =?UTF-8?q?=EA=B0=92=EC=9D=B4=20=EC=95=84=EB=8B=90=20=EA=B2=BD=EC=9A=B0=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EB=8D=98=EC=A7=80=EB=8A=94=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-4/problem-4.test.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index bfe7f91..c520166 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,4 +1,8 @@ -const solution = (input = '') => { +const solution = (input) => { + if (!input) { + throw new TypeError('올바른 2진수 문자열이 아닙니다.'); + } + let result = 0; let number = 0; @@ -10,6 +14,12 @@ const solution = (input = '') => { return result; }; +test('올바른 2진수 문자열을 입력하지 않을 경우 TypeError를 던진다', () => { + expect(() => solution(null)).toThrowError(new TypeError('올바른 2진수 문자열이 아닙니다.')); + expect(() => solution('')).toThrowError(new TypeError('올바른 2진수 문자열이 아닙니다.')); + expect(() => solution()).toThrowError(new TypeError('올바른 2진수 문자열이 아닙니다.')); +}); + test('10진수 숫자를 반환한다', () => { expect(solution('0')).toBe(0); expect(solution('1')).toBe(1);