프로그래머스 lv1. 키패드
keypad = {'1':3, '2':3, '3':3, '4':2, '5':2, '6':2, '7':1, '8':1, '9':1, '*':0,'0':0, '#':0}
def solution(numbers, hand):
answer = ""
last_touched_left = '*'
last_touched_right = '#'
for cur in numbers :
if cur in [1, 4, 7, '*'] :
last_touched_left = str(cur)
answer += "L"
elif cur in [3, 6, 9, '#'] :
last_touched_right = str(cur)
answer += "R"
else :
subtract_left = abs(keypad[last_touched_left] - keypad[str(cur)])
subtract_right = abs(keypad[last_touched_right] - keypad[str(cur)])
if subtract_left < subtract_right :
last_touched_left = str(cur)
answer += "L"
elif subtract_right < subtract_left :
last_touched_right = str(cur)
answer += "R"
else :
if hand == 'right' :
last_touched_right = str(cur)
answer += "R"
else :
last_touched_left = str(cur)
answer += "L"
return answer