-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Merge pull request #2 from YeongseoYoon/main"
- Loading branch information
Showing
6 changed files
with
46 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters