https://programmers.co.kr/learn/courses/30/lessons/67256
방법
키패드를 좌표화하여, 누를 위치와 현재 손 위치 사이의 이동거리를 구하기.
코드
def findClosehand(target, right, left, hand) :
right_dist = abs(right[0] - target[0]) + abs(right[1] - target[1])
left_dist = abs(left[0] - target[0]) + abs(left[1] - target[1])
if right_dist == left_dist :
if hand == "right" : return 'R'
else : return 'L'
elif right_dist < left_dist :
return 'R'
return 'L'
def solution(numbers, hand):
answer = ''
cordinates = {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] }
left_loc = '*'
right_loc = '#'
for number in numbers :
if number in [1,4,7] :
answer += 'L'
left_loc = number
elif number in [3,6,9] :
answer += 'R'
right_loc = number
else :
target = cordinates[number]
right = cordinates[right_loc]
left = cordinates[left_loc]
temp = findClosehand(target, right, left, hand)
answer += temp
if temp == 'R' :
right_loc = number
if temp == 'L' :
left_loc = number
return answer