-
Notifications
You must be signed in to change notification settings - Fork 4
[unn04012] 25.01.02 #10
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| function solution(arr, divisor) { | ||
| const answer = []; | ||
|
|
||
| for (const e of arr) { | ||
| if (e % divisor === 0) answer.push(e); | ||
| } | ||
| if (answer.length === 0) answer.push(-1); | ||
|
|
||
| return answer.sort((a, b) => a - b); | ||
| } | ||
|
|
||
| console.log(solution([5, 9, 7, 10], 5)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| function solution(arr1, arr2) { | ||
| const n = arr1.length; | ||
| const m = arr2[0].length; | ||
|
|
||
| const answer = Array.from({ length: n }, () => Array(m).fill(0)); | ||
|
|
||
| for (let i = 0; i < n; i++) { | ||
| for (let j = 0; j < m; j++) { | ||
| const result = arr1[i].reduce((acc, cur, idx) => acc + cur * arr2[idx][j], 0); | ||
|
|
||
| answer[i][j] = result; | ||
| } | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| console.log( | ||
| solution( | ||
| [ | ||
| [2, 3, 2], | ||
| [4, 2, 4], | ||
| [3, 1, 4], | ||
| ], | ||
| [ | ||
| [5, 4, 3], | ||
| [2, 4, 1], | ||
| [3, 1, 1], | ||
| ] | ||
| ) | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| function solution(numbers, direction) { | ||
| const answer = []; | ||
|
|
||
| if (direction === 'right') { | ||
| for (let i = 0; i < numbers.length - 1; i++) { | ||
| answer[i + 1] = numbers[i]; | ||
| } | ||
| answer[0] = numbers[numbers.length - 1]; | ||
|
Comment on lines
+5
to
+8
Collaborator
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. 현재 for을 사용하여 각 숫자를 이동시키는 것도 좋지만, 혹시 메서드를 사용하면 코드가 단순해져서 이런 방법도 추천드립니다.
|
||
| } else { | ||
| for (let i = numbers.length - 1; i >= 1; i--) { | ||
| answer[i - 1] = numbers[i]; | ||
| } | ||
| answer[numbers.length - 1] = numbers[0]; | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| console.log(solution([4, 455, 6, 4, -1, 45, 6], 'left')); | ||
|
Collaborator
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. 현재 코드에서 for을 사용해도 작동은 하지만, 반복문은 모든 수를 하나씩 순회해야 하기 때문에, 이 문제에서도 불필요하다는 생각이 듭니다. 그런 면에서, slice메서드가 배열의 특정 기간을 쉽게 추출하는 기능을 제공해서 반복문 대신 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| function solution(numbers, num1, num2) { | ||
| const answer = []; | ||
|
|
||
| for (let i = num1; i <= num2; i++) answer.push(numbers[i]); | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| console.log(solution([1, 2, 3, 4, 5], 1, 3)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| function solution(arr) { | ||
| const answer = []; | ||
| const minNum = Math.min(...arr); | ||
|
|
||
| for (const e of arr) { | ||
| if (e === minNum) continue; | ||
| answer.push(e); | ||
| } | ||
|
Comment on lines
+2
to
+8
Collaborator
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. 새로운 배열 answer를 생성하고, for문을 이용해서 조건을 체크하고 숫자들을 추가하고 있지만, 이 부분에서 새로운 배열을 생성하면서 조건도 체크해주는 filter 메서드도 한번 사용해보시는 것도 좋을것 같습니다:) |
||
| return answer.length ? answer : [-1]; | ||
| } | ||
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.
저는 많이 헤맨 문제였는데...Array.from을 사용한 배열 초기화한 부분과 reduce활용으로 깔끔하게 잘 구현된 코드인것 같습니다👍👍