키패드 누르기 - 2020 카카오 인턴십 프로그래머스 ( JavaScript )
|2021. 9. 28. 01:09
반응형
최대한 직관적이게 소스코드를 만들어 보았습니다.
1. 키패드 모양의 배열을 만든다. (* = 11, # = 22 의로 임의의 숫자를 만들어 숫자 타입 만들기)
2. 왼손 오른손의 위치를 지정한 배열을 만든다.
3. 눌러야 할 위치값 측정
4. 손의 위치값과 눌러야 할 위치값 비교
5. 손 위치 변경 후 값 리턴
계산을 위한 함수 2개를 추가로 만들었습니다.
find_num_loca = 눌러야할 위치값 측정 함수
check_lr = 왼손 오른손 중 선택 하는 함수
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
function solution(numbers, hand) {
// numbers 배열 hand = right or left
var answer = '';
const numArr = [[1,2,3],[4,5,6],[7,8,9],[11,0,22]];// * = 11, # = 22 키패드 만들기
const numLen = numbers.length;
let lhLocation = [3,0];
let rhLocation = [3,2];
for(let i = 0; i < numLen; i++){
let numLocation = find_num_loca(numArr,numbers[i]); // 위치 좌표값 받기
answer += check_lr(numLocation,lhLocation,rhLocation,hand);
console.log(numLocation);
console.log(lhLocation);
}
return answer;
}
function find_num_loca(numArr,checkNum){
let retR = 0;
let retC = 0;
for(let i = 0; i < numArr.length; i++){
for(let j = 0; j < numArr[0].length; j++){
if( numArr[i][j] === checkNum){
return [i, j];
}
}
}
}
function check_lr(numLocation,lhLocation,rhLocation,hand){
if(numLocation[1] === 0){
lhLocation[0] = numLocation[0];
lhLocation[1] = numLocation[1];
return 'L';
}else if(numLocation[1] === 2){
rhLocation[0] = numLocation[0];
rhLocation[1] = numLocation[1];
return 'R';
}
else{ // numLocation = 1 중간위치
let leftSize = Math.abs(numLocation[0]-lhLocation[0]) + Math.abs(numLocation[1]-lhLocation[1]);
let rightSize = Math.abs(numLocation[0]-rhLocation[0]) + Math.abs(numLocation[1]-rhLocation[1]);
if(leftSize < rightSize){
lhLocation[0] = numLocation[0];
lhLocation[1] = numLocation[1];
return 'L';
}else if(rightSize < leftSize){
rhLocation[0] = numLocation[0];
rhLocation[1] = numLocation[1];
return 'R';
}else{
if(hand === 'left'){
lhLocation[0] = numLocation[0];
lhLocation[1] = numLocation[1];
}
else {
rhLocation[0] = numLocation[0];
rhLocation[1] = numLocation[1];
}
return hand.substr(0,1).toUpperCase();
}
}
}
|
cs |
반응형
'프로그래밍 > HTML,CSS ,PHP,JQuery,Javascript' 카테고리의 다른 글
PHP CRUD 모듈자료 입력/수정/삭제 모듈 - MSSQL_Version (2) | 2021.06.07 |
---|---|
PHP POST content-length 파일 업로드 용량 오류. (0) | 2021.02.17 |
LED 저항값 계산기 (블로그) - 아두이노 (0) | 2020.09.20 |
PHP 로그인 화면 구성 FORM 태그 POST 전송 mysql (0) | 2020.09.04 |
PHP form 전송 방법 POST / GET 이용방법 (0) | 2020.09.04 |