티스토리 뷰

728x90
반응형

문제 링크

코드

def solution(numbers, hand):

    # 현재 왼쪽, 오른쪽 엄지의 위치를 계속 저장
    curLeft = '*'
    curRight = '#'

    # 2, 5, 8, 0에서 각 번호까지의 거리가 몇인지 dict로 나타냄.
    midDicttwo = {1: ['1', '3', '5'], 2: ['4', '8', '6'], 3: ['7', '9', '0'], 4: ['*', '#']}
    midDicttfive = {1: ['2', '4', '6', '8'], 2: ['1', '3', '7', '9', '0'], 3: ['*', '#']}
    midDictteight = {1: ['5', '7', '0', '9'], 2: ['2', '4', '6', '*', '#'], 3: ['1', '3']}
    midDicttzero = {1: ['8', '*', '#'], 2: ['5', '7', '9'], 3: ['2', '4', '6'], 4: ['1', '3']}

    # 왼쪽, 오른쪽, 가운데의 수
    left = [1, 4, 7]
    right = [3, 6, 9]
    mid = [2, 5, 8, 0]

    answer = ''
    for number in numbers:

        # 왼쪽이면
        if number in left:
            answer += 'L'
            curLeft = str(number)

        # 오른쪽이면
        elif number in right:
            answer += 'R'
            curRight = str(number)

        # 가운데면
        if number in mid:

            # 2이면
            if number == 2:

                # 오른쪽, 왼쪽 손가락과 2와의 거리.
                rightDistance = 0
                leftDistance = 0

                # key 값(거리) 1, 2, 3, 4에서
                for key in midDicttwo.keys():

                    # 오른쪽 엄지가 있다면 그 키를 저장. 즉 거리를 저장
                    if curRight in midDicttwo[key]:
                        rightDistance = key

                    # 왼쪽 엄지가 있다면 그 키를 저장. 즉 거리를 저장
                    if curLeft in midDicttwo[key]:
                        leftDistance = key

                # 거리비교해서 왼쪽 엄지로 누를지 오른쪽 엄지로 누를지 판단.
                if rightDistance > leftDistance:
                    answer += 'L'
                    curLeft = str(number)
                elif rightDistance < leftDistance:
                    answer += 'R'
                    curRight = str(number)
                else:

                    # 같으면 왼손잡이, 오른손잡이로 판단. 
                    if hand == 'left':
                        answer += 'L'
                        curLeft = str(number)
                    else:
                        answer += 'R'
                        curRight = str(number)

            # 5일 때. 위처럼 똑같이 돌아감. 
            elif number == 5:
                rightDistance = 0
                leftDistance = 0
                for key in midDicttfive.keys():
                    if curRight in midDicttfive[key]:
                        rightDistance = key
                    if curLeft in midDicttfive[key]:
                        leftDistance = key
                if rightDistance > leftDistance:
                    answer += 'L'
                    curLeft = str(number)
                elif rightDistance < leftDistance:
                    answer += 'R'
                    curRight = str(number)
                else:
                    if hand == 'left':
                        answer += 'L'
                        curLeft = str(number)
                    else:
                        answer += 'R'
                        curRight = str(number)

            # 8일 때. 위처럼 똑같이 돌아감.
            elif number == 8:
                rightDistance = 0
                leftDistance = 0
                for key in midDictteight.keys():
                    if curRight in midDictteight[key]:
                        rightDistance = key
                    if curLeft in midDictteight[key]:
                        leftDistance = key
                if rightDistance > leftDistance:
                    answer += 'L'
                    curLeft = str(number)
                elif rightDistance < leftDistance:
                    answer += 'R'
                    curRight = str(number)
                else:
                    if hand == 'left':
                        answer += 'L'
                        curLeft = str(number)
                    else:
                        answer += 'R'
                        curRight = str(number)

            # 0일 때. 위처럼 똑같이 돌아감.
            elif number == 0:
                rightDistance = 0
                leftDistance = 0
                for key in midDicttzero.keys():
                    if curRight in midDicttzero[key]:
                        rightDistance = key
                    if curLeft in midDicttzero[key]:
                        leftDistance = key
                if rightDistance > leftDistance:
                    answer += 'L'
                    curLeft = str(number)
                elif rightDistance < leftDistance:
                    answer += 'R'
                    curRight = str(number)
                else:
                    if hand == 'left':
                        answer += 'L'
                        curLeft = str(number)
                    else:
                        answer += 'R'
                        curRight = str(number)
    return answer
728x90
반응형
댓글
반응형
250x250
글 보관함
최근에 달린 댓글
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Total
Today
Yesterday
링크