https://school.programmers.co.kr/learn/courses/30/lessons/67256
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
level.1, 정답률 50%
어려웠던 점
- 키패드의 x, y좌표를 잘못 설정해서 한참 찾음
- 대, 소문자 틀려서 한참 찾음.
- 삼항연산자 사용법 (다중 연산시 return은 맨뒤에) 숙지 필요
삼항연산자에서 return은 마지막 표현식의 결과만 반환되므로
return hand === 'right' ? (true, rightP = keypad[givenKey]) : (false, leftP = keypad[givenKey]);
이렇게 하면 true, false가 반환되지 않는다.
그래서 아래처럼 적어줘야 한다.
return hand === 'right' ? (rightP = keypad[givenKey], true) : (leftP = keypad[givenKey], false);
아니면 그냥 이렇게
if (hand === 'right') {
rightP = keypad[givenKey];
return true;
} else {
leftP = keypad[givenKey];
return false;
}
바로 아래는 나의 풀이.
코드가 다소 길어서, 좀더 단순화 시킬 필요가 있다.
function solution(numbers, hand) {
//좌표화 시켜야함.
//손가락 현위치 저장 필요.
//keyPad의 index가 키번호
const keypad = [[1, 0], [0, 3], [1, 3], [2, 3], [0, 2], [1, 2], [2, 2], [0, 1], [1, 1], [2, 1]]
let leftP = [0, 0];
let rightP = [2, 0];
let answer = [];
//true하면 오른손(right)
function compare(currLeft, currRight, givenKey, hand) {
const leftDifferece = Math.abs(currLeft[0] - keypad[givenKey][0]) + Math.abs(currLeft[1] - keypad[givenKey][1]);
const rightDifferece = Math.abs(currRight[0] - keypad[givenKey][0]) + Math.abs(currRight[1] - keypad[givenKey][1]);
if (leftDifferece > rightDifferece) {
rightP = keypad[givenKey];
return true;
} else if (leftDifferece < rightDifferece) {
leftP = keypad[givenKey];
return false;
} else {
if (hand === 'right') {
rightP = keypad[givenKey];
return true;
} else {
leftP = keypad[givenKey];
return false;
}
// return hand === 'right' ? (true, rightP = keypad[givenKey]) : (false, leftP = keypad[givenKey]);
}
}
for (n of numbers) {
switch (n) {
case 0:
compare(leftP, rightP, 0, hand) ? answer.push('R') : answer.push('L');
break;
case 1:
answer.push('L');
leftP = keypad[1];
break;
case 2:
compare(leftP, rightP, 2, hand) ? answer.push('R') : answer.push('L');
break;
case 3:
answer.push('R');
rightP = keypad[3];
break;
case 4:
answer.push('L');
leftP = keypad[4];
break;
case 5:
compare(leftP, rightP, 5, hand) ? answer.push('R') : answer.push('L');
break;
case 6:
answer.push('R');
rightP = keypad[6];
break;
case 7:
answer.push('L');
leftP = keypad[7];
break;
case 8:
compare(leftP, rightP, 8, hand) ? answer.push('R') : answer.push('L');
break;
case 9:
answer.push('R');
rightP = keypad[9];
break;
case 0:
compare(leftP, rightP, 0, hand) ? answer.push('R') : answer.push('L');
break;
}
}
return answer.join('');
}
console.log(solution([7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2], 'left'));
그리고 아래는 참고할만한 풀이
function solution(numbers, hand) {
hand = hand[0] === "r" ? "R" : "L"
let position = [1, 4, 4, 4, 3, 3, 3, 2, 2, 2]
let h = { L: [1, 1], R: [1, 1] }
return numbers.map(x => {
if (/[147]/.test(x)) {
h.L = [position[x], 1]
return "L"
}
if (/[369]/.test(x)) {
h.R = [position[x], 1]
return "R"
}
let distL = Math.abs(position[x] - h.L[0]) + h.L[1]
let distR = Math.abs(position[x] - h.R[0]) + h.R[1]
if (distL === distR) {
h[hand] = [position[x], 0]
return hand
}
if (distL < distR) {
h.L = [position[x], 0]
return "L"
}
h.R = [position[x], 0]
return "R"
}).join("")
}
regExp.text라는 매소드가 있는지 처음 알았다.
다만 이게 이해가 너무 좀 힘들어서,
아래 풀이가 좀더 도움이 될것같다.
const solution = (numbers, hand) => {
const answer = [];
let leftHandPosition = '*';
let rightHandPosition = '#';
numbers.forEach(number => {
if (number === 1 || number === 4 || number === 7) {
answer.push('L');
leftHandPosition = number;
return;
}
if (number === 3 || number === 6 || number === 9) {
answer.push('R');
rightHandPosition = number;
return;
}
const leftHandDistance = getDistance(leftHandPosition, number);
const rightHandDistance = getDistance(rightHandPosition, number);
if (leftHandDistance === rightHandDistance) {
if (hand === "right") {
answer.push('R');
rightHandPosition = number;
return;
}
if (hand === 'left') {
answer.push('L');
leftHandPosition = number;
return;
}
}
if (leftHandDistance > rightHandDistance) {
answer.push('R');
rightHandPosition = number;
}
if (leftHandDistance < rightHandDistance) {
answer.push('L');
leftHandPosition = number;
}
});
return answer.join("");
};
const getDistance = (locatedNumber, target) => {
const keyPad = {
1: [0, 0], 2: [0, 1], 3: [0, 2],
4: [1, 0], 5: [1, 1], 6: [1, 2],
7: [2, 0], 8: [2, 1], 9: [2, 2],
'*': [3, 0], 0: [3, 1], '#': [3, 2],
}
const nowPosition = keyPad[locatedNumber];
const targetPosition = keyPad[target];
return (Math.abs(targetPosition[0] - nowPosition[0]) +
Math.abs(targetPosition[1] - nowPosition[1]));
};
내용은 내 풀이와 거의 비슷한데, array대신 map을 이용해서 좀더 직관적인 풀이가 된것 같음.