-
Notifications
You must be signed in to change notification settings - Fork 20
1주차 과제 제출 #17
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
memeseo
wants to merge
8
commits into
CodeSoom:main
Choose a base branch
from
memeseo:memeseo
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주차 과제 제출 #17
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
98d8399
Feat. problem-1
memeseo c59b968
Feat. problem-2
memeseo 11bacce
Feat. problem-3
memeseo 04e57f3
Feat. problem-4
memeseo cd499d9
Feat. problem-5
memeseo dd42eba
Feat. problem-6
memeseo 738e2ad
Fix. 피드백 수정
memeseo 9908a9e
Fix. 피드백 반영
memeseo 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
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,4 +1,50 @@ | ||
| //1. 가장 익숙한 방법으로 문제를 해결해 주세요. | ||
| const solution1 = (n) => { | ||
| if(n < 2) return n.toString(); | ||
|
|
||
| let result = []; | ||
|
|
||
| while(true){ | ||
| if(n < 2){ | ||
| result.push(n); | ||
| return result.reverse().join(''); | ||
| } | ||
|
|
||
| const remainder = n % 2; | ||
| result.push(remainder); | ||
| n = Math.floor(n / 2); | ||
| } | ||
| }; | ||
|
|
||
| //2. 이번에는 재귀 함수로 문제를 해결해 주세요. | ||
| const solution2 = (n) => { | ||
| if(n < 2) return n.toString(); | ||
|
|
||
| const remainder = n % 2, | ||
| quotient = Math.floor(n / 2); | ||
|
|
||
| return `${solution2(quotient)}${remainder.toString()}`; | ||
|
|
||
| }; | ||
|
|
||
| //3. 꼬리 재귀 함수로 바꿔보세요. | ||
| const solution3 = (n, result = '') => { | ||
| if(n < 2) return `${n}${result}`; | ||
|
|
||
| return solution3(Math.floor(n / 2), n % 2 + result); | ||
| }; | ||
|
|
||
| //4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. | ||
| const solution = (n) => { | ||
| let result = ''; | ||
|
|
||
| while(true){ | ||
| if(n < 2) return `${n}${result}`; | ||
|
|
||
| const remainder = n % 2; | ||
| n = Math.floor(n / 2); | ||
|
Comment on lines
+44
to
+45
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. 나머지를 remainder로 했다면, 몫은 quotient라는 이름으로 해도 괜찮겠네요 |
||
| result = `${remainder}${result}`; | ||
| } | ||
| }; | ||
|
|
||
| test('이진수 문자열을 반환한다', () => { | ||
|
|
||
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
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는 모두 꼬리 재귀로 변환할 수 있습니다. 두 가지 동작하는 방식을 생각해보면 사실 똑같이 생겼죠