From c08b9709f7fefec343740153a6a1a2c44daa8581 Mon Sep 17 00:00:00 2001 From: whoodongpyo Date: Mon, 8 Jan 2024 17:40:13 +0900 Subject: [PATCH 1/8] =?UTF-8?q?feat:=20=EC=B4=9D=ED=95=A9=20=EA=B5=AC?= =?UTF-8?q?=ED=95=98=EA=B8=B0=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 | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index c775a02..92282d3 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,4 +1,51 @@ +// 1. 가장 익숙한 방법으로 문제를 해결해 주세요. +//const solution = (numbers) => { +// let answer = 0; +// +// for (let i = 0; i < numbers.length; i += 1) { +// answer += numbers[i]; +// } +// +// return answer; +//}; + +// 2. 이번에는 재귀 함수로 문제를 해결해 주세요. +//const solution = (numbers) => { +// // Base Case +// if (numbers.length === 0) { +// return 0; +// } +// +// // Recursive Case +// return numbers.pop() + solution(numbers); +//}; + +// 3. 꼬리 재귀 함수로 바꿔보세요. +// -> Maximum call stack size exceeded 발생.. +//const solution = (numbers, acc = 0) => { +// if (numbers.length === 0) { +// return acc; +// } +// +// // return solution(numbers.slice(0, numbers.length - 1), acc + numbers[numbers.length - 1]); +// return solution(numbers, acc + numbers.pop()); +//}; + +// 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. const solution = (numbers) => { + // 1) 계속해서 바뀌는 값을 변수로 선언한다. + const current = numbers; + let acc = 0; + + // 3) 반복문을 만든다. + while (true) { + if (current.length === 0) { + return acc; + } + + // 2) 재귀 함수 호출로 값을 변경하는 대신에 변수의 값을 할당해서 변경한다. + acc = acc + current.pop(); + } }; test('빈 배열은 0을 반환한다', () => { From c0432e3762db48ee15c84e4208e3d270d0bb414c Mon Sep 17 00:00:00 2001 From: whoodongpyo Date: Mon, 8 Jan 2024 21:11:04 +0900 Subject: [PATCH 2/8] =?UTF-8?q?feat:=20=ED=94=BC=EB=B3=B4=EB=82=98?= =?UTF-8?q?=EC=B9=98=20=EC=88=98=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 | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 458a6b8..28c07da 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -1,4 +1,69 @@ +// 1. 가장 익숙한 방법으로 문제를 해결해 주세요. +// const solution = (n) => { +// if (n <= 0) { +// return 0; +// } +// +// if (n === 1) { +// return 1; +// } +// +// const fibonacci = [0, 1]; +// +// for (let i = 2; i <= n; i += 1) { +// fibonacci[i] = fibonacci[i - 2] + fibonacci[i - 1]; +// } +// +// return fibonacci[n]; +// }; + +// 2. 이번에는 재귀 함수로 문제를 해결해 주세요. +// const solution = (n) => { +// if (n <= 0) { +// return 0; +// } +// +// if (n === 1) { +// return 1; +// } +// +// return solution(n - 2) + solution(n - 1); +// }; + +// 3. 꼬리 재귀 함수로 바꿔보세요. +// const solution = (n, prev = 0, curr = 1) => { +// if (n <= 0) { +// return prev; +// } +// +// if (n === 1) { +// return curr; +// } +// +// return solution(n - 1, curr, prev + curr); +// }; + +// 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. const solution = (n) => { + let number = n; + let prev = 0; + let curr = 1; + let acc = 1; + + while (true) { + if (number <= 0) { + return prev; + } + + if (number === 1) { + return curr; + } + + acc = prev + curr; + prev = curr; + curr = acc; + number -= 1; + } }; test('음수가 주어지면 0을 반환한다', () => { From 220575ce894e6e6b3639399f76e062bdf567226a Mon Sep 17 00:00:00 2001 From: whoodongpyo Date: Wed, 10 Jan 2024 19:16:13 +0900 Subject: [PATCH 3/8] =?UTF-8?q?feat:=202=EC=A7=84=EC=88=98=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=ED=99=98=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-3/problem-3.test.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index 7319eb8..3239e1e 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -1,4 +1,30 @@ -const solution = (n) => { +// const solution = (n) => { +// if (n === 0) { +// return '0'; +// } +// +// let number = n; +// let binaryString = ''; +// +// while (number > 0) { +// const remainder = number % 2; +// binaryString = remainder + binaryString; +// number = Math.floor(number / 2); +// } +// +// return binaryString; +// }; + +const solution = (n, binaryString = '') => { + if (n === 0) { + return '0'; + } + + if (n === 1) { + return `1${binaryString}`; + } + + return solution(Math.floor(n / 2), (n % 2).toString() + binaryString); }; test('이진수 문자열을 반환한다', () => { From aafb86022fb7d8ec45cc9a6643703c3c84ae596f Mon Sep 17 00:00:00 2001 From: whoodongpyo Date: Thu, 11 Jan 2024 18:38:41 +0900 Subject: [PATCH 4/8] =?UTF-8?q?feat:=20=EC=9D=B4=EC=A7=84=EC=88=98=2010?= =?UTF-8?q?=EC=A7=84=EC=88=98=EB=A1=9C=20=EB=B3=80=ED=99=98=ED=95=98?= =?UTF-8?q?=EA=B8=B0=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 | 65 ++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 19f5cb0..7cfeb63 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,4 +1,67 @@ -const solution = () => { +// const solution = (binaryString) => { +// let decimalNumber = 0; +// for (let bitIndex = 0; bitIndex < binaryString.length; bitIndex += 1) { +// const currentBit = parseInt(binaryString[bitIndex], 10); +// const bitWeight = 2 ** (binaryString.length - 1 - bitIndex); +// +// const currentBitValue = currentBit * bitWeight; +// decimalNumber += currentBitValue; +// } +// +// return decimalNumber; +// }; + +// const solution = ( +// binaryString, +// currentIndex = 0, +// bitWeight = binaryString.length - 1, +// ) => { +// if (binaryString === '0') { +// return 0; +// } +// +// if (binaryString === '1') { +// return 1; +// } +// +// if (bitWeight < 0) { +// return 0; +// } +// +// const currentBit = parseInt(binaryString[currentIndex], 10); +// const currentBitValue = (2 ** bitWeight) * currentBit; +// +// return currentBitValue + solution( +// binaryString, +// currentIndex + 1, +// bitWeight - 1, +// ); +// }; + +const solution = ( + binaryString, + currentIndex = 0, + bitWeight = binaryString.length - 1, + decimalNumber = 0, +) => { + if (binaryString === '0') { + return 0; + } + + if (binaryString === '1') { + return 1; + } + + if (bitWeight < 0) { + return decimalNumber; + } + + return solution( + binaryString, + currentIndex + 1, + bitWeight - 1, + decimalNumber + (2 ** bitWeight) * parseInt(binaryString[currentIndex], 10), + ); }; test('10진수 숫자를 반환한다', () => { From 0c711ba39db26f8ab1fcba37dde637ad7a0ac413 Mon Sep 17 00:00:00 2001 From: whoodongpyo Date: Sat, 13 Jan 2024 15:58:56 +0900 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20=EC=9C=A0=ED=81=B4=EB=A6=AC?= =?UTF-8?q?=EB=93=9C=20=ED=98=B8=EC=A0=9C=EB=B2=95=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=B5=9C=EB=8C=80=20=EA=B3=B5=EC=95=BD=EC=88=98=20=EA=B5=AC?= =?UTF-8?q?=ED=95=98=EA=B8=B0=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 | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/problem-5/problem-5.test.js b/problem-5/problem-5.test.js index 20a8fab..e59e56e 100644 --- a/problem-5/problem-5.test.js +++ b/problem-5/problem-5.test.js @@ -1,4 +1,27 @@ -const solution = () => { +/* +const solution = (num1, num2) => { + let smallerNumber = num1; + let largerNumber = num2; + + while (true) { + const remainder = largerNumber % smallerNumber; + + if (remainder === 0) { + return smallerNumber; + } + + largerNumber = smallerNumber; + smallerNumber = remainder; + } +}; +*/ + +const solution = (smallerNumber, largerNumber, remainder = largerNumber % smallerNumber) => { + if (remainder === 0) { + return smallerNumber; + } + + return solution(remainder, smallerNumber); }; test('최대 공약수를 반환한다', () => { From 2b28f2bc8e48207f4cfb3634bc42b3b9da6627a8 Mon Sep 17 00:00:00 2001 From: whoodongpyo Date: Sat, 13 Jan 2024 17:29:22 +0900 Subject: [PATCH 6/8] =?UTF-8?q?feat:=20=EA=B3=84=EB=8B=A8=20=EC=98=A4?= =?UTF-8?q?=EB=A5=B4=EA=B8=B0=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 | 45 ++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index 059e2b8..c36f19e 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -1,4 +1,47 @@ -const solution = (n) => { +// const solution = (n) => { +// if (n < 0) { +// return 0; +// } +// +// if (n === 0 || n === 1) { +// return 1; +// } +// +// return solution(n - 3) + solution(n - 2) + solution(n - 1); +// }; + +// 하향식 다이내믹 프로그래밍 (메모이제이션) + +// const solution = (n, memo = []) => { +// if (n < 0) { +// return 0; +// } +// if (n === 0 || n === 1) { +// return 1; +// } +// +// if (!memo[n]) { +// memo[n] = solution(n - 3, memo) + solution(n - 2, memo) + solution(n - 1, memo); +// } +// +// return memo[n]; +// }; + +// 상향식 다이내믹 프로그래밍 +const solution = (n, current = 2, a = 0, b = 1, c = 1) => { + if (n < 0) { + return 0; + } + + if (n === 0 || n === 1) { + return 1; + } + + if (n === current) { + return a + b + c; + } + + return solution(n, current + 1, b, c, a + b + c); }; test('계단에 오를 수 있는 가지 수를 반환한다', () => { From 8c7daa8e7a27ff37de3720024d3189997df947b7 Mon Sep 17 00:00:00 2001 From: whoodongpyo Date: Mon, 15 Jan 2024 15:04:41 +0900 Subject: [PATCH 7/8] =?UTF-8?q?refactor:=20=EC=B4=9D=ED=95=A9=20=EA=B5=AC?= =?UTF-8?q?=ED=95=98=EA=B8=B0=20-=20=EB=B0=B0=EC=97=B4=EC=9D=84=20?= =?UTF-8?q?=EC=A7=81=EC=A0=91=20=EC=88=98=EC=A0=95=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=8C=80=EC=8B=A0=20index=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EA=B8=B0=20-=20ESLint=20=EA=B2=BD=EA=B3=A0=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0=20:=20while(true)=20=EC=99=80=20=EA=B0=99=EC=9D=80=20?= =?UTF-8?q?=EC=83=81=EC=88=98=20=EC=A1=B0=EA=B1=B4=EC=9D=84=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EB=8A=94=20=EA=B2=83=EC=9D=84=20=EB=B0=A9?= =?UTF-8?q?=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-1/problem-1.test.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index 92282d3..4006f53 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,5 +1,5 @@ // 1. 가장 익숙한 방법으로 문제를 해결해 주세요. -//const solution = (numbers) => { +// const solution = (numbers) => { // let answer = 0; // // for (let i = 0; i < numbers.length; i += 1) { @@ -7,10 +7,10 @@ // } // // return answer; -//}; +// }; // 2. 이번에는 재귀 함수로 문제를 해결해 주세요. -//const solution = (numbers) => { +// const solution = (numbers) => { // // Base Case // if (numbers.length === 0) { // return 0; @@ -18,34 +18,33 @@ // // // Recursive Case // return numbers.pop() + solution(numbers); -//}; +// }; // 3. 꼬리 재귀 함수로 바꿔보세요. // -> Maximum call stack size exceeded 발생.. -//const solution = (numbers, acc = 0) => { +// const solution = (numbers, acc = 0) => { // if (numbers.length === 0) { // return acc; // } // // // return solution(numbers.slice(0, numbers.length - 1), acc + numbers[numbers.length - 1]); // return solution(numbers, acc + numbers.pop()); -//}; +// }; // 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. const solution = (numbers) => { // 1) 계속해서 바뀌는 값을 변수로 선언한다. - const current = numbers; let acc = 0; + let index = 0; // 3) 반복문을 만든다. - while (true) { - if (current.length === 0) { - return acc; - } - + while (index < numbers.length) { // 2) 재귀 함수 호출로 값을 변경하는 대신에 변수의 값을 할당해서 변경한다. - acc = acc + current.pop(); + acc = acc + numbers[index]; + index += 1; } + + return acc; }; test('빈 배열은 0을 반환한다', () => { From 4753e25bd956033fdc2d06d141f29dc1fe66668b Mon Sep 17 00:00:00 2001 From: whoodongpyo Date: Mon, 15 Jan 2024 15:30:26 +0900 Subject: [PATCH 8/8] =?UTF-8?q?refactor:=20=ED=94=BC=EB=B3=B4=EB=82=98?= =?UTF-8?q?=EC=B9=98=20=EC=88=98=20-=20ESLint=20=EA=B2=BD=EA=B3=A0=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0=20:=20while=20(true)=20=EC=99=80=20=EA=B0=99?= =?UTF-8?q?=EC=9D=80=20=EC=83=81=EC=88=98=20=EC=A1=B0=EA=B1=B4=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=20=EB=B0=A9=EC=A7=80=20-=20=EB=B3=80=EC=88=98?= =?UTF-8?q?=EB=AA=85=EC=9D=84=20=EC=9D=98=EB=AF=B8=EC=9E=88=EB=8A=94=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-2/problem-2.test.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 28c07da..e39822e 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -45,25 +45,23 @@ // 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. const solution = (n) => { - let number = n; - let prev = 0; - let curr = 1; - let acc = 1; + let currentNumber = n; + let previousValue = 0; + let currentValue = 1; + let nextValue; - while (true) { - if (number <= 0) { - return prev; + while (currentNumber > 0) { + if (currentNumber === 1) { + return currentValue; } - if (number === 1) { - return curr; - } - - acc = prev + curr; - prev = curr; - curr = acc; - number -= 1; + nextValue = previousValue + currentValue; + previousValue = currentValue; + currentValue = nextValue; + currentNumber -= 1; } + + return previousValue; }; test('음수가 주어지면 0을 반환한다', () => {