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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

### Level 1 ✅

- 전체 문제 수: 55문제
- 풀이 문제 수: 55문제
- 전체 문제 수: 56문제
- 풀이 문제 수: 56문제

| 번호 | 문제 출처 | 풀이 |
| --- | ------- | --- |
Expand Down Expand Up @@ -77,6 +77,7 @@
| 53 | [하샤드 수](https://school.programmers.co.kr//learn/courses/30/lessons/12947) | [하샤드-수.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-1/하샤드-수.js) |
| 54 | [핸드폰 번호 가리기](https://school.programmers.co.kr//learn/courses/30/lessons/12948) | [핸드폰-번호-가리기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-1/핸드폰-번호-가리기.js) |
| 55 | [행렬의 덧셈](https://school.programmers.co.kr//learn/courses/30/lessons/12950) | [행렬의-덧셈.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-1/행렬의-덧셈.js) |
| 56 | [성격 유형 검사하기](https://school.programmers.co.kr/learn/courses/30/lessons/118666) | [성격-유형-검사하기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-1/성격-유형-검사하기.js) |

### Level 2 👨🏻‍💻(풀이 중..)

Expand Down
45 changes: 45 additions & 0 deletions level-1/성격-유형-검사하기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//https://github.com/codeisneverodd/programmers-coding-test
//더 좋은 풀이가 존재할 수 있습니다.

//정답 1 - ssi02014
function solution(survey, choices) {
const points = [3, 2, 1, 0, 1, 2, 3];
const pointBoard = {
R: 0,
T: 0,
C: 0,
F: 0,
J: 0,
M: 0,
A: 0,
N: 0,
};
let result = "";

// 카테고리 별 점수 추가
for (let i = 0; i < survey.length; i++) {
const categories = survey[i];

if (choices[i] < 4) {
pointBoard[categories[0]] += points[choices[i] - 1];
} else if (choices[i] > 4) {
pointBoard[categories[1]] += points[choices[i] - 1];
}
}
Comment on lines +7 to +28
Copy link
Owner

Choose a reason for hiding this comment

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

"함수형 프로그래밍"

pointBoard의 키가 적기 때문에 위와 같은 방법으로 할 수 있습니다. 다만 키가 많아지는 경우 위와 같이 직접 적어주기 어렵고, 코드 또한 간결하지 않게됩니다. pointBoard를 만들기 위한 방법으로 다음과 같은 방법도 고려해보세요!

7-28라인을 아래와 같이 간단하게 나타낼 수 있습니다. 단, 모든 키값의 생성이나, 순서를 보장하지 않습니다.

const pointBoard = survey.reduce((a, c, i) => {
  const key = choices[i] < 4 ? c[0] : c[1];
  a[key] = (a[key] || 0) + points[choices[i] - 1];
  return a;
}, {});

Copy link
Contributor Author

@ssi02014 ssi02014 Aug 29, 2022

Choose a reason for hiding this comment

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

좋은 풀이 잘봤습니다 🙇‍♂️ 단순 문제 요구사항에 맞추는걸 넘어서 다른 부분까지 채운 풀이를 하셨군요 👍

정말 깔끔하네요 도움이 많이 됐습니다 그런데 이번처럼 깔끔하게 나오는거 제외하고 가끔 저는 명령형이 오히려 가독성이 좋을 때도 있어서 꼭 선언형(함수형) 처럼 작성해야되냐?는 고민이 좀 되네요 🤔 어떻게 생각하시나요??

Copy link
Owner

Choose a reason for hiding this comment

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

동의합니다! 명령형이 오히려 가독성이 좋을때도 있다고 생각해요 😄
하지만 대부분의 경우 함수형(선언형 프로그래밍의 일종)이 더 가독성이 좋을 때가 많다고 생각해요! 명령형은 코드의 흐름이 길어질 수록 파악하기 어려운 경우가 많은 것 같습니다.
함수형의 가독성이 떨어지는 경우는 고차함수에 대한 이해도가 부족한 것에서 오는 경우가 많은 것 같아요!
프론트엔드를 희망하고 계신다면 함수형으로 작성해야할 경우가 많기 때문에 저는 함수형으로 최대한 풀어보려고 노력하고 있습니다!!
코테를 볼 때는 생각이 잘 안나서 명령형으로 풀 때도 있지만, 리팩토링 할 때는 반드시 함수형으로 바꾸려 노력하는 편이에요 👍


const pointBoardEntries = Object.entries(pointBoard);

// 지표에 맞게 결과 값 도출
for (let i = 0; i < pointBoardEntries.length; i += 2) {
const [curCategory, curValue] = pointBoardEntries[i];
const [nextCategory, nextValue] = pointBoardEntries[i + 1];
Comment on lines +33 to +35
Copy link
Owner

Choose a reason for hiding this comment

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

"객체는 순서를 보장하지 않음"

Object 객체는 순서를 보장하지 않습니다.
대부분의 모던 브라우저는 기존의 순서를 유지하고, 이로부터 생성된 Entries도 그 순서가 유지되기는 하지만 순서를 확신할 수 없습니다.
따라서 위와 같은 방식은 다른 환경에서 동작하지 않을 가능성이 있기 때문에, pointBoard를 배열로서 선언하여 사용하는 방식이나, 서로 연관있는 값들을 순서가 아닌 객체로서 묶어서 사용하는 방식을 권장드립니다.

Copy link
Contributor Author

@ssi02014 ssi02014 Aug 29, 2022

Choose a reason for hiding this comment

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

피드백 감사합니다 말씀듣고 배열로도 좋고 Map 생성자 사용해도 좋을 것 같다는 생각이 들었네요 👍
그런데 Object가 삽입 순서를 보장하지 않는다는 알고있었는데, 이 문제에서처럼 초기에 선언할 때도 그게 관련있는건지 😮

Copy link
Owner

Choose a reason for hiding this comment

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

'선언할 때 key가 바뀌지 않는다는 말'을 이해하지 못했어요! 다시 설명해주실 수 있나요? @ssi02014

Copy link
Owner

Choose a reason for hiding this comment

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

제가 알기론 ECMAScript 상의 지침이 없고, 브라우저마다 다른 것으로 알고있습니다! test 를 유사배열객체로 선언하지 않는 이상 숫자로 되어있는 key 값들도 순서의 의미를 가지지 않기 때문에, 브라우저마다 서로 다른 key 순서를 가져도 무방할 것 같습니다! 애초에 Object 의 key 값들에는 순서가 없으니까요!


if (curValue < nextValue) {
result += nextCategory;
} else {
result += curCategory;
}
}

return result;
}