Algorithm

[프로그래머스] 키패드 누르기 (Python)

salmon16 2021. 2. 15. 18:39

출처 : 코딩테스트 연습 - 키패드 누르기 | 프로그래머스 (programmers.co.kr)

 

코딩테스트 연습 - 키패드 누르기

[1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5] "right" "LRLLLRLLRRL" [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2] "left" "LRLLRRLLLRR" [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] "right" "LLRLLRLLRL"

programmers.co.kr

풀이 방법

*, 0, #은 먼저 10, 11, 12로 매칭을 시킨다.

1, 4, 7은 따로 left_num이라는 배열에 저장을 해두고 3, 6, 9는 right_num이라는 배열에 저장해두고

numbers 배열을 돌아가며 숫자가 각 배열에 있을 시 answer에 추가를 해주고 각 최근 left 와 right를 초기화시켜준다.

나머지 숫자 2, 5, 8, 0 은 마지막 left와 right에서 떨어진 거리를 계산하는 함수를 작성하여 거리를 구한 후

거리가 작은 쪽 손으로 누르는 것으로 처리해 준다. 

거리가 같을 시 손잡이에 따라 손 선택

left_num = [1, 4, 7]
right_num = [3, 6, 9]

def count_dist(hand, num):    
    hand_column = 0
    num_column = 0
    ## 문자열 전처리
    if hand == '*':        
        hand_num = 10
    elif hand == '#':
        hand_num = 12
    elif hand == '0':
        hand_num = 11
    elif hand == 0:
        hand_num = 11
    else:
        hand_num = int(hand)        
    if num == '0':
        num = 11
    if num == 0:
        num = 11
    if hand_num == 1 or hand_num == 2 or hand_num == 3:
        hand_column = 0
    if hand_num == 4 or hand_num == 5 or hand_num == 6:
        hand_column = 1
    if hand_num == 7 or hand_num == 8 or hand_num == 9:
        hand_column = 2
    if hand_num == 10 or hand_num == 11 or hand_num == 12:
        hand_column = 3
    if num == 1 or num == 2 or num == 3:
        num_column = 0
    if num == 4 or num == 5 or num == 6:
        num_column = 1
    if num == 7 or num == 8 or num == 9:
        num_column = 2
    if num == 10 or num == 11 or num == 12:
        num_column = 3
    ## row 설정
    hand_row = hand_num % 3
    num_row = num % 3
    if hand_row == 0:
        hand_row = 3
    if num_row == 0:
        num_row = 3
	## 거리 설정    
    return abs(hand_column - num_column) + abs(hand_row - num_row)
    
def solution(numbers, hand):
    answer = ''
    left = '*'
    right = '#'
    for i in range(len(numbers)):
        if numbers[i] in left_num:
            answer += 'L'
            left = str(numbers[i])
        elif numbers[i] in right_num:
            answer += 'R'
            right = str(numbers[i])
        else:            
            r_dist = count_dist(right, numbers[i])
            l_dist = count_dist(left, numbers[i])
            if r_dist > l_dist:
                answer += 'L'
                left = str(numbers[i])
            elif r_dist < l_dist:
                answer += 'R'
                right = str(numbers[i])            
            else:
            	##거리가 같을 시
                if hand == "right":
                    answer += 'R'
                    right = str(numbers[i])            
                else:
                    answer += 'L'
                    left = str(numbers[i])            
    return answer

더 좋은 풀이

키패드의 좌표를 구할때 딕셔너리를 사용하면 더 편리하게 구할수 있다.

key_dict = {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)}