문제출처 : 프로그래머스
문제소개
키패드 누르기
- 왼손 엄지는 1,4,7 을 오른손 엄지는 3,6,9 를 누른다.
- 2,5,8,0 은 누를 키패드와 더 위치한 엄지가 누른다.
- 엄지는 상,하,좌,우로만 움직일 수 있으면 한 칸은 거리 1로 계산
- 거리가 같다면 오른손 잡이는 오른엄지, 왼손 잡이는 왼엄지 사용
입력)
numbers = [1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5]
hand = "right"
출력)
result = "LRLLLRLLRRL"
코드
def solution(numbers, hand):
    answer = ''
    left = 10 
    right = 12
    
    for i in numbers:
        if i in [1,4,7]:
            answer += 'L'
            left = i 
        elif i in [3,6,9]: 
            answer += 'R'
            right = i 
        else: 
            i = 11 if i == 0 else i
            
            absL = abs(i-left)
            absR = abs(i-right)
            
            if sum(divmod(absL,3)) < sum(divmod(absR,3)): 
                answer += 'L'
                left = i 
            elif sum(divmod(absL,3)) > sum(divmod(absR,3)):
                answer += 'R'
                right = i 
            else:
                if hand == 'right':
                    answer += 'R'
                    right = i 
                else: 
                    answer += 'L'
                    left = i
            
    return answer
solution([1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5], 'right')
'LRLLLRLLRRL'
정의된 변수 값 확인
absL = 4
sum(divmod(absL,3))
2
absR = 2 
sum(divmod(absR,3))
2