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
16 changes: 16 additions & 0 deletions problem-1/problem-1.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
// 1. 가장 익숙한 방법으로 문제를 해결해 주세요.
const solution = (numbers) => {
numbers.reduce((a, b) => a + b, 0);
};
Comment on lines 2 to 4
Copy link
Contributor

Choose a reason for hiding this comment

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

여기서 작은 실수를 하나 하셨는데, 결과를 return하고 있지 않네요!


// 2. 이번에는 재귀 함수로 문제를 해결해 주세요.
// 3번째 테스트 통과 못함
// const solution = (numbers, index = 0) => {
// if (index === numbers.length) {
// return 0;
// } else {
// return numbers[index] + solution(numbers, index + 1);
// }
// }

// 3. 꼬리 재귀 함수로 바꿔보세요.
// 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요.
// 1번 풀이와 동일

test('빈 배열은 0을 반환한다', () => {
expect(solution([])).toBe(0);
});
Expand Down
29 changes: 29 additions & 0 deletions problem-2/problem-2.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
// 1. 가장 익숙한 방법으로 문제를 해결해 주세요.
const solution = (n) => {
const list = [1, 1];
let count = 2;

while (count <= n) {
count += 1;
const sum = list.slice(-2).reduce((a, b) => a + b, 0);
list.push(sum);
Copy link
Contributor

Choose a reason for hiding this comment

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

연산할 때 마다 list가 계속 커지는데, 마지막 2개만 사용하는 것 같네요. 전전값과 바로 이전값만 유지하는 방법으로도 구현이 가능할 것 같습니다

}

return n > 0 ? list[n - 1] : 0;
};

// 2. 이번에는 재귀 함수로 문제를 해결해 주세요.
// 4번째 테스트 통과 못함
// const solution = (n) => {
// if (n < 0) {
// return 0;
// }

// if (n === 0 || n === 1) {
// return n;
// }

// return solution(n - 1) + solution(n - 2);
// };

// 3. 꼬리 재귀 함수로 바꿔보세요.
// 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요.
// 1번 풀이와 동일

test('음수가 주어지면 0을 반환한다', () => {
expect(solution(-1)).toBe(0);
});
Expand Down
21 changes: 21 additions & 0 deletions problem-3/problem-3.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
// 1. 가장 익숙한 방법으로 문제를 해결해 주세요.
// const solution = (n) => n.toString(2);

// 2. 이번에는 재귀 함수로 문제를 해결해 주세요.
// 3. 꼬리 재귀 함수로 바꿔보세요.
// 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요.
const solution = (n) => {
let result = '';

while (true) {
if (n === 0) {
return `0${result}`;
}

if (n === 1) {
return `1${result}`;
}

const remainder = `${n % 2}`;
n = Math.floor(n / 2);
result = remainder + result;
}
};

test('이진수 문자열을 반환한다', () => {
Expand Down
24 changes: 23 additions & 1 deletion problem-4/problem-4.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
const solution = () => {
// 1. 가장 익숙한 방법으로 문제를 해결해 주세요.
// const solution = (n) => parseInt(n, 2);

// 2. 이번에는 재귀 함수로 문제를 해결해 주세요.
// 3. 꼬리 재귀 함수로 바꿔보세요.
// 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요.
const solution = (n) => {
let result = 0;

while (true) {
if (n === '0') {
return 0 + result;
}

if (n === '1') {
return 1 + result;
}

const first = n[0];

result += (2 ** (n.length - 1)) * Number(first);
n = n.slice(1);
}
};

test('10진수 숫자를 반환한다', () => {
Expand Down
17 changes: 16 additions & 1 deletion problem-5/problem-5.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
const solution = () => {
// 1. 가장 익숙한 방법으로 문제를 해결해 주세요.
// const solution = (a, b) => (b === 0 ? a : solution(b, a % b));

// 2. 이번에는 재귀 함수로 문제를 해결해 주세요.
// 3. 꼬리 재귀 함수로 바꿔보세요.
// 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요.
const solution = (a, b) => {
while (true) {
if (a % b === 0) {
return b;
}

const newA = a;
a = b;
b = newA % b;
}
};

test('최대 공약수를 반환한다', () => {
Expand Down
15 changes: 14 additions & 1 deletion problem-6/problem-6.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
const solution = (n) => {
const solution = (n, list = {}) => {
if (n < 0) {
return 0;
}

if (n === 0 || n === 1) {
return 1;
}

if (!list[n]) {
list[n] = solution(n - 1, list) + solution(n - 2, list) + solution(n - 3, list);
}

return list[n];
};

test('계단에 오를 수 있는 가지 수를 반환한다', () => {
Expand Down