키패드

suhan cho·2022년 7월 23일
0
def solution(numbers, hand):
    answer = ''

    num = {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 = num['*']
    right = num['#']
    

    for i in numbers:
        now = num[i]
        
        if i in [1, 4, 7]:
            answer += 'L'
            left = now
            
        elif i in [3, 6, 9]:
            answer += 'R'
            right = now
            
        # 2, 5, 8, 0을 누르는 경우
        else:
            left_dist = 0
            right_dist = 0
            
            for a, b, c in zip(left, right, now):
                left_dist += abs(a-c)
                right_dist += abs(b-c)
            
            # 왼손이 더 가까운 경우
            if left_dist < right_dist:
                answer += 'L'
                left = now
                
            # 오른손이 더 가까운 경우
            elif left_dist > right_dist:
                answer += 'R'
                right = now
            
            # 두 거리가 같은 경우
            else:
                # 왼손잡이 경우
                if hand == 'left':
                    answer += 'L'
                    left = now
                    
                # 오른손잡이 경우
                else:
                    answer += 'R'
                    right = now
            
    return answer
profile
안녕하세요

0개의 댓글