-
Notifications
You must be signed in to change notification settings - Fork 20
1주차 과제 제출 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gagyeong17
wants to merge
7
commits into
CodeSoom:main
Choose a base branch
from
gagyeong17:week-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
1주차 과제 제출 #16
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c4c3f52
problem-1/solution-1
gagyeong17 d8c007f
problem-1/solution-2
gagyeong17 16ca807
problem-1/solution-3,4
gagyeong17 96681b3
problem-2/solution-1,2,3,4
gagyeong17 b134acf
problem-5/solution-1,2,3,4
gagyeong17 198800c
problem-6/solution/1,2
gagyeong17 9bd2c57
problem--3,4
gagyeong17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,18 +1,49 @@ | ||
| // 베이스 케이스를 찾는다. | ||
| // 베이스 케이스 이전의 단계를 찾는다. | ||
| // 베이스 케이스 이전의 이전 단계를 찾는다. | ||
| // 거꾸로 문제를 해결한다. | ||
|
|
||
| const solution = (numbers) => { | ||
| if (!numbers.length) return 0; | ||
| // 1. 가장 익숙한 방법으로 문제를 해결해 주세요. | ||
| const solution1 = numbers.reduce((prev, cur) => prev + cur, 0); | ||
| return solution1; | ||
|
|
||
| // 2. 이번에는 재귀 함수로 문제를 해결해 주세요. | ||
| const solution2 = (arr, idx = 0) => { | ||
| if (idx >= arr.length) return 0; | ||
| return arr[idx] + solution2(arr, idx + 1); | ||
| }; | ||
| return solution2(numbers); | ||
|
|
||
| // 3. 꼬리 재귀 함수로 바꿔보세요. | ||
| // 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. | ||
| const solution3 = () => { | ||
| let sum = 0; | ||
| let arr = [...numbers]; | ||
| while (true) { | ||
| if (!arr.length) return sum; | ||
| const first = arr[0]; | ||
| sum += first; | ||
| arr = arr.slice(1); | ||
| } | ||
| }; | ||
|
Comment on lines
+21
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const solution3 = () => {
let sum = 0;
let arr = [...numbers];
let first;
while (true) {
if (arr.length === 0) {
return sum;
}
[first, ...arr] = arr;
sum += first;
}
};자바스크립트의 구조 분해 할당을 사용하면 이렇게도 할 수 있겠네요 |
||
| return solution3(numbers); | ||
| }; | ||
|
|
||
| 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 hidden or 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,26 +1,51 @@ | ||
| const solution = (n) => { | ||
| if (n <= 0) return 0; | ||
| if (n === 1) return 1; | ||
| // 1. 가장 익숙한 방법으로 문제를 해결해 주세요. | ||
| // 2. 이번에는 재귀 함수로 문제를 해결해 주세요. | ||
| return solution(n - 2) + solution(n - 1); | ||
|
|
||
| // 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. | ||
| let current = 2; | ||
| let a = 0; | ||
| let b = 1; | ||
| while (true) { | ||
| if (n === current) return a + b; | ||
| current += 1; | ||
| const temp = a; | ||
| a = b; | ||
| b = temp + b; | ||
|
Comment on lines
+15
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요것도 구조 분해 할당을 사용하면 좀 더 간단하게 표현해볼 수 있겠어요 [a, b] = [b, a + b] |
||
| } | ||
| }; | ||
| // 3. 꼬리 재귀 함수로 바꿔보세요. | ||
| const solution = (n, current = 2, a = 0, b = 1) => { | ||
| if (n <= 0) return 0; | ||
| if (n === 1) return 1; | ||
| if (n === current) return a + b; | ||
| return solution(n, current + 1, b, a + b); | ||
| }; | ||
|
|
||
| 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 hidden or 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,21 +1,30 @@ | ||
| const solution = (n) => { | ||
| // 1. 가장 익숙한 방법으로 문제를 해결해 주세요. | ||
| // const solution1 = n.toString(2); | ||
| // return solution1; | ||
| // 2. 이번에는 재귀 함수로 문제를 해결해 주세요. | ||
| if (n === 0 || n === 1) return n.toString(); | ||
| const quotient = Math.floor(n / 2); | ||
| const remainder = (n % 2).toString(); | ||
| return solution(quotient) + remainder; | ||
| }; | ||
|
|
||
| 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 hidden or 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,21 +1,26 @@ | ||
| const solution = () => { | ||
| const solution = (n) => { | ||
| // 1. 가장 익숙한 방법으로 문제를 해결해 주세요. | ||
| const solution1 = parseInt(n, 2); | ||
|
|
||
| return solution1; | ||
| }; | ||
|
|
||
| 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 hidden or 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,17 +1,30 @@ | ||
| const solution = () => { | ||
| const solution = (a, b) => { | ||
| // 1. 가장 익숙한 방법으로 문제를 해결해 주세요. | ||
| // 2. 이번에는 재귀 함수로 문제를 해결해 주세요. | ||
| // 3. 꼬리 재귀 함수로 바꿔보세요. | ||
| if (a % b === 0) return b; | ||
| return solution(b, a % b); | ||
| // 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. | ||
| while (true) { | ||
| if (a % b === 0) return b; | ||
| const temp = a; | ||
| a = b; | ||
| b = temp % b; | ||
| } | ||
| }; | ||
|
|
||
| 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
꼬리재귀로 작성된 코드는 모두 reduce를 사용한 코드로 변환할 수 있습니다. 이전 값을 다음 값으로 넘기는 부분이 꼬리 재귀랑 똑같죠.
그리고 reduce에 파라미터 이름은 누산기(accumulator)라는 이름으로
acc를 자주 사용합니다.