Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions imchanyo/[week1]Array/Length_of_elements_in_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function solution(strlist) {
var answer = [];

strlist.forEach((string) => answer.push(string.length));
return answer;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 방식도 좋지만 map 함수도 사용해보면 좋을 것 같아요! 그러면 좀 더 간결해집니다!

12 changes: 12 additions & 0 deletions imchanyo/[week1]Array/Matrix_addition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function solution(arr1, arr2) {
let answer = [];
for (let i = 0; i < arr1.length; i++) {
let newArr = [];
for (let j = 0; j < arr1[0].length; j++) {
newArr.push(arr1[i][j] + arr2[i][j]);
}
answer.push(newArr);
}

return answer;
}
11 changes: 11 additions & 0 deletions imchanyo/[week1]Array/Number_of_duplicate_numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function solution(array, n) {
var answer = [];

array.forEach((num) => {
if (num === n) {
answer.push(num);
}
});

return answer.length;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드 길이를 줄이고 싶으시다면 filter라는 함수를 사용해도 좋을 것 같아요! 성능도 비슷하고!

Comment on lines +1 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배열을 순회하면서 특정 수의 개수를 세는 것이니까, 한번만 순회하면서 답을 구하는 건 좋은 것같아요. 그런데 정답을 도출하기위해 배열하나를 더해서 push작업을 수행하고 있는데, push도 결국 배열길이가 늘어가면 비용이 커지니까 이 문제에서는 reduce나 filter를 사용하는 것이 더 좋지 않을까 합니다. 특히 이 문제는 값하나(개수)를 도출해내는 것이니까 reduce가 제일 간단할 것 같아요

const solution = (array, n) => {
    return array.reduce((acc,cur) => {
        return cur === n ? ++acc : acc;
    },0)
}

}
17 changes: 17 additions & 0 deletions imchanyo/[week1]Array/Remove_smallest_number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function solution(arr) {
var answer = [];

if (arr[0] === 10) {
return [-1];
}
Comment on lines +4 to +6
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

왜 첫번째 배열이 10인경우 [-1]return하도록 하신걸까용?
문제에서 [-1]return하는 경우는 작은 수를 제거했을 때 빈 배열만 남는 경우인 것 같아서요!


for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[i] > arr[j]) {
answer.push(arr[i]);
}
}
}
Comment on lines +8 to +14
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 하면 시간 복잡도가 올라갈 것 같은데, 최소값을 먼저 구하고 그걸로 배열을 가공하면 복잡도가 줄어들지 않을까 생각합니다!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

같은 생각입니다!


return [...new Set(answer)];
}
38 changes: 38 additions & 0 deletions imchanyo/[week1]Array/Rotate_array.js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function solution(numbers, direction) {
var answer = [...numbers];

if (direction === "right") {
const rightNumbers = answer.pop();
answer.unshift(rightNumbers);
} else if (direction === "left") {
const leftNumbers = answer.shift();
answer.push(leftNumbers);
}

return answer;
}

/* slice 연습하기 */
function solution(numbers, direction) {
if (direction === "right") {
const last = numbers.slice(-1);
const rest = numbers.slice(0, -1);
return [...last, ...rest];
} else if (direction === "left") {
const first = numbers.slice(0, 1);
const rest = numbers.slice(1);
return [...rest, ...first];
}
}
Comment on lines +16 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slice함수에 음수 인덱스를 사용할 수 있는 것 리마인드 하고 갑니다 :)
연습하신 것이겠지만, 맨앞,맨마지막 요소를 구하기 위해 slice를 쓰는 것 보다는 배열인덱스로 바로 접근하는게 slice메서드 호출을 줄여줘서 좋을 것 같습니다.

const solution = (numbers, direction) => {
    if (direction === 'right'){
        return [numbers[numbers.length - 1], ...numbers.slice(0,numbers.length - 1)];
    }
    else{
        return [...numbers.slice(1), numbers[0]];
    }
}


/* splice 연습 */
function solution(numbers, direction) {
if (direction === "right") {
const last = numbers.splice(-1, 1);
numbers.unshift(...last);
} else if (direction === "left") {
const first = numbers.splice(0, 1);
numbers.push(...first);
}
return numbers;
}
5 changes: 5 additions & 0 deletions imchanyo/[week1]Array/Trim_Array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function solution(numbers, num1, num2) {
var answer = [];
answer = numbers.splice(num1, num2);
return answer;
}
Loading