From 98d8399b9b5b96f4144e69f6d0e34238d96b94de Mon Sep 17 00:00:00 2001 From: umbrellamoss Date: Tue, 16 Jul 2024 23:34:55 +0900 Subject: [PATCH 1/8] Feat. problem-1 --- problem-1/problem-1.test.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index c775a02..282e35d 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,6 +1,38 @@ -const solution = (numbers) => { +// 1. 가장 익숙한 방법으로 문제를 해결해 주세요. +const solution1 = (numbers) => { + if(numbers?.length === 0) return 0; + + return numbers.reduce((acc, cur) => acc + cur, 0); }; +// 2. 이번에는 재귀 함수로 문제를 해결해 주세요. +// Maximum call stack size exceeded 오류나는데 왜 나는지 모르겠습니다 +const solution2 = (numbers) => { + if(numbers?.length === 0) return 0; + + return numbers[0] + solution2(numbers.slice(1)); +}; + +// 3. 꼬리 재귀 함수로 바꿔보세요. +// Maximum call stack size exceeded 오류나는데 왜 나는지 모르겠습니다 +const solution3 = (numbers, acc = 0) => { + if(numbers?.length === 0) return acc; + + return solution3(numbers.slice(1), acc + numbers[0]); +} + +// 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. +const solution = (numbers) => { + if(numbers?.length === 0) return 0; + + let acc = 0; + for(let number of numbers){ + acc += number; + } + + return acc; +} + test('빈 배열은 0을 반환한다', () => { expect(solution([])).toBe(0); }); From c59b968460e223d61aa9ea9ea61b9925aa689c2d Mon Sep 17 00:00:00 2001 From: umbrellamoss Date: Tue, 16 Jul 2024 23:35:05 +0900 Subject: [PATCH 2/8] Feat. problem-2 --- problem-2/problem-2.test.js | 50 ++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 458a6b8..a934b17 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -1,6 +1,54 @@ -const solution = (n) => { + +// 1. 가장 익숙한 방법으로 문제를 해결해 주세요. +const solution1 = (n) => { + if(n <= 0) return 0; + if(n === 1) return 1; + + let array = [0, 1]; + + for(let i=2; i <= n; i+=1){ + array[i] = array[i - 2] + array[i - 1]; + } + + return array[n]; }; +//2. 이번에는 재귀 함수로 문제를 해결해 주세요. +const solution2 = (n) => { + if(n <= 0) return 0; + if(n === 1) return 1; + + return solution2(n-2) + solution2(n-1); +} + +//3. 꼬리 재귀 함수로 바꿔보세요. +const solution3 = (n, prev = 0, curr = 1) => { + if(n <= 0) return prev; + if(n === 1) return curr; + + return solution3(n-1, curr, prev + curr); + +} + +//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. +const solution = (n) => { + let param = n, + prev = 0, + curr = 1, + sumResult = 0; + + while(true){ + if(param <= 0) return prev; + if(param === 1) return curr; + + sumResult = prev + curr; + prev = curr; + curr = sumResult; + param -= 1; + } +} + + test('음수가 주어지면 0을 반환한다', () => { expect(solution(-1)).toBe(0); }); From 11baccebf6b193e13913cfdd451158141d7598f1 Mon Sep 17 00:00:00 2001 From: umbrellamoss Date: Tue, 16 Jul 2024 23:35:19 +0900 Subject: [PATCH 3/8] Feat. problem-3 --- problem-3/problem-3.test.js | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index 7319eb8..d575eab 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -1,4 +1,50 @@ +//1. 가장 익숙한 방법으로 문제를 해결해 주세요. +const solution1 = (n) => { + if(n < 2) return n.toString(); + + let result = []; + + while(true){ + if(n < 2){ + result.push(n); + return result.reverse().join(''); + } + + const remainder = n % 2; + result.push(remainder); + n = Math.floor(n / 2); + } +}; + +//2. 이번에는 재귀 함수로 문제를 해결해 주세요. +const solution2 = (n) => { + if(n < 2) return n.toString(); + + const remainder = n % 2, + quotient = Math.floor(n / 2); + + return `${solution2(quotient)}${remainder.toString()}`; + +}; + +//3. 꼬리 재귀 함수로 바꿔보세요. +const solution3 = (n, result = '') => { + if(n < 2) return `${n}${result}`; + + return solution3(Math.floor(n / 2), n % 2 + result); +}; + +//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. const solution = (n) => { + let result = ''; + + while(true){ + if(n < 2) return `${n}${result}`; + + const remainder = n % 2; + n = Math.floor(n / 2); + result = `${remainder}${result}`; + } }; test('이진수 문자열을 반환한다', () => { From 04e57f35edc24aa8eb6842bcf8c9afcdb7e625e9 Mon Sep 17 00:00:00 2001 From: umbrellamoss Date: Tue, 16 Jul 2024 23:35:31 +0900 Subject: [PATCH 4/8] Feat. problem-4 --- problem-4/problem-4.test.js | 55 ++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 19f5cb0..9ee63b7 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,4 +1,57 @@ -const solution = () => { +//1. 가장 익숙한 방법으로 문제를 해결해 주세요. +const solution1 = (binaryString) => { + const array = binaryString.split('').map(Number); + let result = 0; + + for(let i = 0; i < array.length; i++){ + const num = array.length - (i + 1); + result += array[i] * (2 ** num); + } + + return result; + }; + +// 2. 이번에는 재귀 함수로 문제를 해결해 주세요. +const solution2 = (binaryString) => { + + if (binaryString === '0') return 0; + if (binaryString === '1') return 1; + + const array = binaryString.split('').map(Number); + const num = array[0] * ( 2 ** (array.length - 1)); + + return num + solution2(array.slice(1).join('')); +}; + +// 3. 꼬리 재귀 함수로 바꿔보세요. +const solution3 = (binaryString, num = 0) => { + + if(binaryString.length === 1) return num + Number(binaryString); + + const array = binaryString.split('').map(Number); + num += array[0] * (2 ** (array.length - 1)); + + return solution3(array.slice(1).join(''), num); +}; + +// 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. +const solution = (binaryString) => { + + let array = binaryString.split('').map(Number); + let result = 0; + + while(true){ + if (array.toString() === '0') { + return 0 + result; + } + + if (array.toString() === '1') { + return 1 + result; + } + + result += array[0] * (2 ** (array.length - 1)); + array = array.slice(1); + } }; test('10진수 숫자를 반환한다', () => { From cd499d9290a788fa7e28f8612cc454ee59f5cee6 Mon Sep 17 00:00:00 2001 From: umbrellamoss Date: Tue, 16 Jul 2024 23:35:46 +0900 Subject: [PATCH 5/8] Feat. problem-5 --- problem-5/problem-5.test.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/problem-5/problem-5.test.js b/problem-5/problem-5.test.js index 20a8fab..f1006c5 100644 --- a/problem-5/problem-5.test.js +++ b/problem-5/problem-5.test.js @@ -1,4 +1,35 @@ -const solution = () => { +//1. 가장 익숙한 방법으로 문제를 해결해 주세요. +const solution1 = (a, b) => { + if(b === 0) return a; + + return solution1(b, a % b); +}; + +//2. 이번에는 재귀 함수로 문제를 해결해 주세요. +const solution2 = (a, b) => { + if(b === 0) return a; + + return solution2(b, a % b); +}; + +//3. 꼬리 재귀 함수로 바꿔보세요. +const solution3 = (a, b) => { + if(b === 0) return a; + + return solution3(b, a % b); +}; + +//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. +const solution = (a, b) => { + while (true) { + if (a % b === 0) { + return b; + } + + const temp = a; + a = b; + b = temp % b; + } }; test('최대 공약수를 반환한다', () => { From dd42ebad537222e4f25199c085a3db399cddf79d Mon Sep 17 00:00:00 2001 From: umbrellamoss Date: Tue, 16 Jul 2024 23:35:54 +0900 Subject: [PATCH 6/8] Feat. problem-6 --- problem-6/problem-6.test.js | 42 ++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index 059e2b8..4241bc3 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -1,4 +1,44 @@ -const solution = (n) => { +//1. 재귀 함수로 문제를 해결해 주세요. +const solution1 = (n) => { + if (n < 0) { + return 0; + } + + if (n === 0 || n === 1) { + return 1; + } + return solution1(n - 3) + solution1(n - 2) + solution1(n - 1); +}; +//2. 다이나믹 프로그래밍으로 최적화 해주세요. +const solution2 = (n, memo = []) => { + if (n < 0) { + return 0; + } + + if (n === 0 || n === 1) { + return 1; + } + + if(!memo[n]){ + memo[n] = solution2(n - 3, memo) + solution2(n - 2, memo) + solution2(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 738e2ad03d892e7c43fc1f51756ecda5eb5410bb Mon Sep 17 00:00:00 2001 From: umbrellamoss Date: Sat, 20 Jul 2024 15:27:06 +0900 Subject: [PATCH 7/8] =?UTF-8?q?Fix.=20=ED=94=BC=EB=93=9C=EB=B0=B1=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - numbers 배열 옵셔널 체이닝 > 기본값 설정으로 변경 --- problem-1/problem-1.test.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index 282e35d..b1c5ab6 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,29 +1,29 @@ // 1. 가장 익숙한 방법으로 문제를 해결해 주세요. -const solution1 = (numbers) => { - if(numbers?.length === 0) return 0; +const solution1 = (numbers = []) => { + if(numbers.length === 0) return 0; return numbers.reduce((acc, cur) => acc + cur, 0); }; // 2. 이번에는 재귀 함수로 문제를 해결해 주세요. // Maximum call stack size exceeded 오류나는데 왜 나는지 모르겠습니다 -const solution2 = (numbers) => { - if(numbers?.length === 0) return 0; +const solution2 = (numbers = []) => { + if(numbers.length === 0) return 0; return numbers[0] + solution2(numbers.slice(1)); }; // 3. 꼬리 재귀 함수로 바꿔보세요. // Maximum call stack size exceeded 오류나는데 왜 나는지 모르겠습니다 -const solution3 = (numbers, acc = 0) => { - if(numbers?.length === 0) return acc; +const solution3 = (numbers = [], acc = 0) => { + if(numbers.length === 0) return acc; return solution3(numbers.slice(1), acc + numbers[0]); } // 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. -const solution = (numbers) => { - if(numbers?.length === 0) return 0; +const solution = (numbers = []) => { + if(numbers.length === 0) return 0; let acc = 0; for(let number of numbers){ From 9908a9ebd6a13ab708a5f0b9072d7a6324641004 Mon Sep 17 00:00:00 2001 From: umbrellamoss Date: Sat, 20 Jul 2024 16:22:33 +0900 Subject: [PATCH 8/8] =?UTF-8?q?Fix.=20=ED=94=BC=EB=93=9C=EB=B0=B1=20?= =?UTF-8?q?=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 이진수 배열 생성 후 10진수 변환 > parseInt 함수 사용 및 인덱스 위치로 10진수 변환 --- problem-4/problem-4.test.js | 49 ++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 9ee63b7..ec54a71 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,56 +1,55 @@ //1. 가장 익숙한 방법으로 문제를 해결해 주세요. const solution1 = (binaryString) => { - const array = binaryString.split('').map(Number); let result = 0; - for(let i = 0; i < array.length; i++){ - const num = array.length - (i + 1); - result += array[i] * (2 ** num); + for(let i = 0; i < binaryString.length; i++){ + const binaryNumber = parseInt(binaryString[i], 10), + index = binaryString.length - (i + 1); + + result += binaryNumber * (2 ** index); } return result; }; // 2. 이번에는 재귀 함수로 문제를 해결해 주세요. -const solution2 = (binaryString) => { - - if (binaryString === '0') return 0; +const solution2 = (binaryString, binaryIndex = 0, stringIndex = binaryString.length - 1) => { + if (binaryString === '0' || stringIndex < 0) return 0; if (binaryString === '1') return 1; - const array = binaryString.split('').map(Number); - const num = array[0] * ( 2 ** (array.length - 1)); + const binaryNumber = parseInt(binaryString[binaryIndex], 10), + result = binaryNumber * ( 2 ** stringIndex); - return num + solution2(array.slice(1).join('')); + return result + solution2(binaryString, binaryIndex + 1, stringIndex - 1); }; // 3. 꼬리 재귀 함수로 바꿔보세요. -const solution3 = (binaryString, num = 0) => { +const solution3 = (binaryString, binaryIndex = 0, stringIndex = binaryString.length - 1, result = 0) => { + if (binaryString === '0') return 0; + if (binaryString === '1') return 1; + if (stringIndex < 0) return result; - if(binaryString.length === 1) return num + Number(binaryString); - - const array = binaryString.split('').map(Number); - num += array[0] * (2 ** (array.length - 1)); - - return solution3(array.slice(1).join(''), num); + return solution3(binaryString, binaryIndex + 1, stringIndex - 1, result + (2 ** stringIndex) * parseInt(binaryString[binaryIndex], 10)); }; // 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. const solution = (binaryString) => { - - let array = binaryString.split('').map(Number); - let result = 0; + let result = 0, + stringIndex = binaryString.length - 1, + binaryIndex = 0; - while(true){ - if (array.toString() === '0') { + while (true) { + if (binaryString === '0' || stringIndex < 0) { return 0 + result; } - if (array.toString() === '1') { + if (binaryString === '1') { return 1 + result; } + result += (2 ** stringIndex) * parseInt(binaryString[binaryIndex], 10); - result += array[0] * (2 ** (array.length - 1)); - array = array.slice(1); + stringIndex--; + binaryIndex++; } };