-
Notifications
You must be signed in to change notification settings - Fork 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
[실습] 연습문제 week1/03 제출합니다 #14
Open
minsoo-web
wants to merge
3
commits into
main
Choose a base branch
from
practice/example-minsoo-03
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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,15 +1,17 @@ | ||
export function multiDimensionalAccumulate( | ||
multiDimensionalArr: number[][], | ||
): number { | ||
let accumulator = 0 | ||
const sum = (a: number, b: number): number => a + b | ||
|
||
for (let i = 0; i < multiDimensionalArr.length; i += 1) { | ||
for (let j = 0; j < multiDimensionalArr[i].length; j += 1) { | ||
if (j < i) { | ||
accumulator += multiDimensionalArr[i][j] | ||
} | ||
} | ||
} | ||
|
||
return accumulator | ||
/** | ||
* 접근 방법: | ||
* 커밋내역을 보시면 아시겠지만 ㅋㅋ큐ㅠㅠ 문제를 완전 잘못 이해하고 풀어버려서 | ||
* 다시 풀었습니다. | ||
* | ||
* 다시 풀었을 때 첫 접근 방법은 2중 reduce문을 사용해서 변수명을 직관적으로 지으면서 가독성을 높혔었는데 | ||
* 그럼 기존의 2중 for 문이랑 크게 다르지 않다고 생각해서 n^2 시간 복잡도를 개선해봤습니다. | ||
* flatMap을 활용해서, 대각선 이하의 숫자들만 잘라내고 | ||
* 그 값들을 더했습니다. | ||
*/ | ||
export const multiDimensionalAccumulate = (arr: number[][]): number => { | ||
return arr | ||
.flatMap((innerArray, rowIndex) => innerArray.slice(0, rowIndex)) | ||
.reduce(sum, 0) | ||
} |
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.
혹시 왜 두줄로 하셨는지 궁금해요
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.
순수함수니까 실행 횟수와 관계없이, 같은 input에서 같은 output이 나오는 것을 테스트하고자 했습니다!
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.
아하! 그러면 함수를 두 번 호출하는 것이 2번의 기준으로
strict mode
를 의미하는 것으로 추측하였는데 이유를 들을 수 있을까요? 테스트 횟수에 대한 기준이 있는 것인지 궁금해요. 테스트를 접한지 얼마되지 않아서 실수로 느껴졌었거든요.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.
아 저도 저런 식으로 똑같이 적는 방법은 이번이 처음이어서 2번이냐 몇 번이냐는 크게 중요하진 않았던 것 같고,
multiDimensionalArr
을 암묵적 출력으로 변경시키지 않는다는 걸 보여주고 싶어서 일부러 변수로 선언하고 두 번 호출해봤던 것 같습니다.지금 생각해보면 실행 후의
multiDimensionalArr
이 실행 전과 동일하다라는 걸 테스트 하는 게 좀 더 명확했겠네요