diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index c775a02..166fd25 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,5 +1,36 @@ -const solution = (numbers) => { -}; +// const solutionEasy = (numbers) => { +// let acc = 0; + +// numbers.forEach((cur) => { +// acc += cur; +// }); + +// return acc; +// }; + +// const solutionRecursive = (numbers) => { +// if (numbers.length === 0) { +// return 0; +// } + +// return numbers.pop() + solutionRecursive(numbers); +// }; + +// const solutionLoop = (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'), + ); }); 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을 반환한다', () => { 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('이진수 문자열을 반환한다', () => { 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('계단에 오를 수 있는 가지 수를 반환한다', () => {