Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions problem-1/problem-1.test.js
Original file line number Diff line number Diff line change
@@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numbers.pop()은 원본 배열을 수정하는 메서드입니다. 만약 다른 곳에서 numbers를 참조하고 있었다면 예상하지 못한 일이 일어날 수 있을 것 같아요.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇군요! 원본 데이터에 직접 접근하고 있네요! 감사합니다!

// }

// return acc;
// };

const solution = (numbers) => numbers.reduce((acc, cur) => acc + cur, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

꼬리 재귀 함수는 모두 reduce함수로 구현이 가능하다는 것을 이용해 구현하셨네요 ㅎㅎ 좋습니다


test('빈 배열은 0을 반환한다', () => {
expect(solution([])).toBe(0);
Expand All @@ -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'),
);
});
43 changes: 43 additions & 0 deletions problem-2/problem-2.test.js
Original file line number Diff line number Diff line change
@@ -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을 반환한다', () => {
Expand Down
11 changes: 10 additions & 1 deletion problem-3/problem-3.test.js
Original file line number Diff line number Diff line change
@@ -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('이진수 문자열을 반환한다', () => {
Expand Down
35 changes: 34 additions & 1 deletion problem-6/problem-6.test.js
Original file line number Diff line number Diff line change
@@ -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('계단에 오를 수 있는 가지 수를 반환한다', () => {
Expand Down