Skip to content

Commit

Permalink
Revert "Merge pull request #2 from YeongseoYoon/main"
Browse files Browse the repository at this point in the history
This reverts commit e5ccb85, reversing
changes made to 4349bc1.
  • Loading branch information
hannut91 committed Sep 4, 2023
1 parent e5ccb85 commit 407ca25
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 274 deletions.
49 changes: 5 additions & 44 deletions problem-1/problem-1.test.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,18 @@
//1.가장 익숙한 방법으로 문제를 해결해 주세요.
{
/*
const solution = (numbers) => {
let sum = 0;
if (numbers.length === 0) {
return 0;
}
for (let i = 0; i < numbers.length; i++) {
sum += parseInt(numbers[i], 10);
}
return sum;
};*/
}
//2.이번에는 재귀 함수로 문제를 해결해 주세요.
{
/*
const solution = (numbers, index = 0) => {
if (index >= numbers.length) {
return 0;
} else {
return numbers[index] + solution(numbers, index + 1);
}
};
*/
}

//3. 꼬리 재귀 함수로 바꿔보세요.
//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요.
const solution = (numbers) => {
let index = 0;
let acc = 0;

while (index < numbers.length) {
acc = acc + numbers[index];
index++;
}

return acc;
};
test("빈 배열은 0을 반환한다", () => {
test('빈 배열은 0을 반환한다', () => {
expect(solution([])).toBe(0);
});

test("배열의 합을 반환한다", () => {
test('배열의 합을 반환한다', () => {
expect(solution([1, 2, 3, 4])).toBe(10);
expect(solution([-1, 3, 8, 9, 10, 11])).toBe(40);
});

test("큰 배열이 입력으로 주어져도 RangeError를 던지지 않는다", () => {
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'));
});
45 changes: 6 additions & 39 deletions problem-2/problem-2.test.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,26 @@
//1. 가장 익숙한 방법으로 문제를 해결해 주세요.
//2. 이번에는 재귀 함수로 문제를 해결해 주세요.
{
/*
const solution = (n) => {
if (n <= -1) {
return 0;
} else if (n === 0 || n === 1) {
return n;
} else {
return solution(n - 1) + solution(n - 2);
}
};
*/
}
//3. 꼬리 재귀 함수로 바꿔보세요.
//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요.
const solution = (n) => {
let pre = 0;
let curr = 1;
let temp = 0;
if (n <= -1) {
return 0;
} else if (n === 0 || n === 1) {
return n;
}
while (n >= 2) {
temp = pre;
pre = pre + curr;
curr = temp;
n--;
}
return pre + curr;
};

test("음수가 주어지면 0을 반환한다", () => {
test('음수가 주어지면 0을 반환한다', () => {
expect(solution(-1)).toBe(0);
});

test("0부터 1까지는 정해진 수를 반환한다", () => {
test('0부터 1까지는 정해진 수를 반환한다', () => {
expect(solution(0)).toBe(0);
expect(solution(1)).toBe(1);
});

test("2이상 주어지면 앞 두 항의 합을 반환한다", () => {
test('2이상 주어지면 앞 두 항의 합을 반환한다', () => {
expect(solution(2)).toBe(1);
expect(solution(3)).toBe(2);
expect(solution(4)).toBe(3);
expect(solution(5)).toBe(5);
expect(solution(6)).toBe(8);
});

test("큰 입력이 주어져도 RangeError를 던지지 않는다", () => {
test('큰 입력이 주어져도 RangeError를 던지지 않는다', () => {
const input = 100000;

expect(() => solution(input)).not.toThrowError(
new RangeError("Maximum call stack size exceeded")
);
expect(() => solution(input))
.not.toThrowError(new RangeError('Maximum call stack size exceeded'));
});
69 changes: 13 additions & 56 deletions problem-3/problem-3.test.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,21 @@
//1. 가장 익숙한 방법으로 문제를 해결해 주세요.
{
/*
const solution = (n) => {
let binaryString = "";
if (n <= 1) {
return "" + n;
}
while (n >= 1) {
binaryString += (n % 2).toString();
n = parseInt(n / 2);
}
return binaryString.split("").reverse().join("");
};*/
}
//2. 이번에는 재귀 함수로 문제를 해결해 주세요.
{
/*
const solution = (n, binaryString = "") => {
if (n <= 1) {
return n + binaryString;
}
const remainder = n % 2;
binaryString = remainder + binaryString;
return solution(parseInt(n / 2, 10), binaryString);
};
*/
}

//3. 꼬리 재귀 함수로 바꿔보세요.
//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요.
const solution = (n) => {
let binaryString = "";
if (n <= 1) {
return n + binaryString;
}
while (n >= 1) {
const remainder = n % 2;
binaryString = remainder + binaryString;
n = parseInt(n / 2);
}
return binaryString;
};

test("이진수 문자열을 반환한다", () => {
expect(solution(0)).toBe("0");
expect(solution(1)).toBe("1");
expect(solution(2)).toBe("10");
expect(solution(3)).toBe("11");
expect(solution(4)).toBe("100");
expect(solution(5)).toBe("101");
expect(solution(6)).toBe("110");
expect(solution(7)).toBe("111");
expect(solution(8)).toBe("1000");
test('이진수 문자열을 반환한다', () => {
expect(solution(0)).toBe('0');
expect(solution(1)).toBe('1');
expect(solution(2)).toBe('10');
expect(solution(3)).toBe('11');
expect(solution(4)).toBe('100');
expect(solution(5)).toBe('101');
expect(solution(6)).toBe('110');
expect(solution(7)).toBe('111');
expect(solution(8)).toBe('1000');
});

test("큰 입력이 주어져도 RangeError를 던지지 않는다", () => {
test('큰 입력이 주어져도 RangeError를 던지지 않는다', () => {
const input = Number.MAX_VALUE;

expect(() => solution(input)).not.toThrowError(
new RangeError("Maximum call stack size exceeded")
);
expect(() => solution(input))
.not.toThrowError(new RangeError('Maximum call stack size exceeded'));
});
77 changes: 14 additions & 63 deletions problem-4/problem-4.test.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,21 @@
//1. 가장 익숙한 방법으로 문제를 해결해 주세요.
{
/*
const solution = (n) => {
let decimal = 0;
let exponent = 0;
if (n === "0" || n === "1") {
return parseInt(n, 10);
}
const length = n.length;
while (exponent <= length - 1) {
decimal += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent;
exponent++;
n = n.slice(0, -1);
}
return decimal;
};*/
}
//2. 이번에는 재귀 함수로 문제를 해결해 주세요.
{
/*
const solution = (n, acc = 0, exponent = 0) => {
if (n.length === 1) {
return acc + parseInt(n, 10) * 2 ** exponent;
}
acc += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent;
return solution(n.slice(0, -1), acc, exponent + 1);
};*/
}

//3. 꼬리 재귀 함수로 바꿔보세요.
//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요.
const solution = (n) => {
let acc = 0;
let exponent = 0;

const length = n.length;
if (length === 1) {
return acc + parseInt(n, 10) * 2 ** exponent;
}

while (exponent <= length - 1) {
acc += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent;

exponent++;
n = n.slice(0, -1);
}
return acc;
const solution = () => {
};

test("10진수 숫자를 반환한다", () => {
expect(solution("0")).toBe(0);
expect(solution("1")).toBe(1);
expect(solution("10")).toBe(2);
expect(solution("11")).toBe(3);
expect(solution("100")).toBe(4);
expect(solution("101")).toBe(5);
expect(solution("110")).toBe(6);
expect(solution("111")).toBe(7);
expect(solution("1000")).toBe(8);
test('10진수 숫자를 반환한다', () => {
expect(solution('0')).toBe(0);
expect(solution('1')).toBe(1);
expect(solution('10')).toBe(2);
expect(solution('11')).toBe(3);
expect(solution('100')).toBe(4);
expect(solution('101')).toBe(5);
expect(solution('110')).toBe(6);
expect(solution('111')).toBe(7);
expect(solution('1000')).toBe(8);
});

test("큰 입력이 주어져도 RangeError를 던지지 않는다", () => {
test('큰 입력이 주어져도 RangeError를 던지지 않는다', () => {
const input = Number.MAX_VALUE.toString(2);

expect(() => solution(input)).not.toThrowError(
new RangeError("Maximum call stack size exceeded")
);
expect(() => solution(input))
.not.toThrowError(new RangeError('Maximum call stack size exceeded'));
});
36 changes: 5 additions & 31 deletions problem-5/problem-5.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,17 @@
//1. 가장 익숙한 방법으로 문제를 해결해 주세요.
//2. 이번에는 재귀 함수로 문제를 해결해 주세요.
{
/*
const solution = (b, a) => {
if (a === 0) {
return b;
}
return solution(a, b % a);
const solution = () => {
};
*/
}

//3. 꼬리 재귀 함수로 바꿔보세요.
//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요.
const solution = (b, a) => {
let temp;
while (true) {
if (a === 0) {
return b;
}
temp = a;
a = b % temp;
b = temp;
console.log(b + " " + a);
}
};

test("최대 공약수를 반환한다", () => {
test('최대 공약수를 반환한다', () => {
expect(solution(4, 12)).toBe(4);
expect(solution(3, 7)).toBe(1);
expect(solution(16, 72)).toBe(8);
expect(solution(9, 12)).toBe(3);
});

test("큰 입력이 주어져도 RangeError를 던지지 않는다", () => {
test('큰 입력이 주어져도 RangeError를 던지지 않는다', () => {
const a = Number.MAX_VALUE;
const b = 1213;

expect(() => solution(a, b)).not.toThrowError(
new RangeError("Maximum call stack size exceeded")
);
expect(() => solution(a, b))
.not.toThrowError(new RangeError('Maximum call stack size exceeded'));
});
44 changes: 3 additions & 41 deletions problem-6/problem-6.test.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
//1. 재귀 함수로 문제를 해결해 주세요.
{
/*const solution = (n) => {
if (n === 1) {
return 1;
}
if (n === 2) {
return 2;
}
if (n === 3) {
return 4;
}
return solution(n - 1) + solution(n - 2) + solution(n - 3);
};*/
}

//2. 다이나믹 프로그래밍으로 최적화 해주세요.
const solution = (n, memo = []) => {
if (n === 1) {
return 1;
}

if (n === 2) {
return 2;
}

if (n === 3) {
return 4;
}

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

return memo[n];
const solution = (n) => {
};

test("계단에 오를 수 있는 가지 수를 반환한다", () => {
test('계단에 오를 수 있는 가지 수를 반환한다', () => {
expect(solution(1)).toBe(1);
expect(solution(2)).toBe(2);
expect(solution(3)).toBe(4);
Expand All @@ -52,6 +14,6 @@ test("계단에 오를 수 있는 가지 수를 반환한다", () => {
expect(solution(10)).toBe(274);
});

test("큰 입력이 주어져도 시간안에 실행된다", async () => {
test('큰 입력이 주어져도 시간안에 실행된다', async () => {
expect(solution(40)).toBe(23837527729);
});

0 comments on commit 407ca25

Please sign in to comment.