문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/77484
프로그래머스 - 키패드누르기 Cpp
프로그래머스 - 키패드누르기 Python
def solution(numbers, hand): answer = '' left_hands = [1, 4, 7] right_hands = [3, 6, 9] middle = [2, 5, 8, 0] key_pad = { 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], 0: [3, 1], } left_position = [3, 0] right_position = [3, 2] for key in numbers: if key in left_hands: left_position = key_pad[key] answer += 'L' elif key in right_hands: right_position = key_pad[key] answer += 'R' else: # print(key_pad[key][0]) left_dist = abs( left_position[0] - key_pad[key][0]) + abs(left_position[1] - key_pad[key][1]) right_dist = abs( right_position[0] - key_pad[key][0]) + abs(right_position[1] - key_pad[key][1]) if left_dist == right_dist: if hand == 'right': right_position = key_pad[key] answer += "R" else: left_position = key_pad[key] answer += "L" elif left_dist < right_dist: left_position = key_pad[key] answer += 'L' else: right_position = key_pad[key] answer += 'R' return answer