-
Notifications
You must be signed in to change notification settings - Fork 0
day31 re PR #5
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
lgyn10
wants to merge
1
commit into
main
Choose a base branch
from
day31
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
day31 re PR #5
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // https://school.programmers.co.kr/learn/courses/30/lessons/120887 | ||
|
|
||
| // 최초 코드 | ||
| const k의개수 = (i: number, j: number, k: number): number => { | ||
| let cnt = 0; | ||
| while (i <= j) { | ||
| cnt += [...i.toString()].filter((v) => parseInt(v, 10) === k).length; | ||
| i++; | ||
| } | ||
| return cnt; | ||
| }; | ||
|
|
||
| // 개선 코드 | ||
| const k의개수2 = (i: number, j: number, k: number): number => { | ||
| let str = ''; | ||
| while (i <= j) str += i++; | ||
| // k를 기준으로 문자열을 나누면 k+1개의 요소로 나눠진다. | ||
| return str.split(k.toString()).length - 1; | ||
| }; | ||
|
|
||
| console.log(k의개수(1, 13, 1)); // 6 | ||
| console.log(k의개수(10, 50, 5)); // 5 | ||
| console.log(k의개수(3, 10, 2)); // 0 | ||
|
|
||
| console.log(k의개수2(1, 13, 1)); // 6 | ||
| console.log(k의개수2(10, 50, 5)); // 5 | ||
| console.log(k의개수2(3, 10, 2)); // 0 |
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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // https://school.programmers.co.kr/learn/courses/30/lessons/120864 | ||
|
|
||
| import { sum } from 'lodash'; | ||
|
|
||
| // ver 1. 틀린 코드 | ||
| // /\d/의 경우 한자리 자연수만 검사하게 된다. 문제 의도와 다르다. | ||
| const 숨어있는숫자의덧셈2a = (str: string): number => { | ||
| return sum([...str].filter((v) => /\d/.test(v)).map((v) => parseInt(v, 10))); | ||
| }; | ||
|
|
||
| //! 최적의 코드인 것 같다. | ||
| // ver 2. split 사용 | ||
| const 숨어있는숫자의덧셈2b = (str: string): number => { | ||
| return sum( | ||
| str | ||
| .split(/[a-z]/i) | ||
| .filter((v) => v !== '') | ||
| .map((v) => parseInt(v, 10)), | ||
| ); | ||
| }; | ||
|
|
||
| // ver 3. replace 사용 | ||
| const 숨어있는숫자의덧셈2c = (str: string) => { | ||
| let ans = 0; | ||
| str | ||
| .replace(/\D/g, ' ') | ||
| .split(' ') | ||
| .forEach((v) => { | ||
| if (v !== '') ans += parseInt(v, 10); | ||
| }); | ||
| return ans; | ||
| }; | ||
|
|
||
| // ver 4. 다른 사람 코드 | ||
| // /\D+/g와 reduce 활용 | ||
|
|
||
| console.log(숨어있는숫자의덧셈2a('aAb1B2cC34oOp')); // 37 | ||
| console.log(숨어있는숫자의덧셈2a('1a2b3c4d123Z')); // 133 | ||
|
|
||
| console.log(숨어있는숫자의덧셈2b('aAb1B2cC34oOp')); // 37 | ||
| console.log(숨어있는숫자의덧셈2b('1a2b3c4d123Z')); // 133 | ||
|
|
||
| console.log(숨어있는숫자의덧셈2c('aAb1B2cC34oOp')); // 37 | ||
| console.log(숨어있는숫자의덧셈2c('1a2b3c4d123Z')); // 133 | ||
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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // https://school.programmers.co.kr/learn/courses/30/lessons/120848 | ||
|
|
||
| // ver 1. 최초 코드 | ||
| // 코드가 명시적임 | ||
| // 시간 효율은 ver 2보다 떨어진다. | ||
| const 팩토리얼 = (n: number): number => { | ||
| // 팩토리얼 함수 | ||
| const factorialFunc = (num: number): number => { | ||
| if (num === 1) return 1; | ||
| return num * factorialFunc(num - 1); | ||
| }; | ||
| // 코어 로직 | ||
| let cnt = 0; | ||
| while (factorialFunc(cnt + 1) <= n) cnt++; | ||
| return cnt; | ||
| }; | ||
|
|
||
| // ver 2. 개선 코드 | ||
| // 코드 의미가 명시적이지는 않지만, 시간 효율은 좋다. | ||
| const 팩토리얼2 = (n: number): number => { | ||
| let fac = 1; | ||
| let i = 1; | ||
| while (fac <= n) fac *= ++i; | ||
| return i - 1; | ||
| }; | ||
|
|
||
| console.log(팩토리얼(3628800)); // 10 | ||
| console.log(팩토리얼(7)); // 3 | ||
|
|
||
| console.log(팩토리얼2(3628800)); // 10 | ||
| console.log(팩토리얼2(7)); // 3 | ||
| console.log(팩토리얼2(5)); // 2 |
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.
키워드를 활용하여 코드 작성해보기