Programmers/1단계

[2020 카카오 인턴십] 키패드 누르기

Snowboarder 2022. 10. 19. 14:39
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제

내풀이

def solution(numbers, hand):
    answer = ''
    diction = {1:[0,1],2:[0,2],3:[0,3],4:[1,1],5:[1,2],6:[1,3],7:[2,1],8:[2,2],9:[2,3],10:[3,1],0:[3,2],11:[3,3]}
    left = [1, 4, 7]
    right = [3, 6, 9]
    middle = [2, 5, 8, 0]

    L = 10
    R = 11

    # 경우의수
        #  1 4 7 * 인 경우 -> "L"
        #  3 6 9 # 인 경우 -> "R"
        #  2 5 8 0  (main) 인 경우
            # 거리가 같은 경우
                # hand로 추가
            # 거리가 다른 경우
                # 거리가 짧은 것 선택

    # 거리(main)
        # 거리를 어떻게 구할 것인가
            # L R 값이 재초기화가 계속 되어야한다. 
            # 그 좌표를 가지고 x값들 절댓값 차이 + y값들 차이 절댓값 


    for i in numbers:
        if i in left:
            answer += "L"
            L = i

        elif i in right:
            answer  += "R"
            R = i

        elif i in middle:
            Ldis = abs(diction.get(L)[0] -diction.get(i)[0]) +abs(diction.get(L)[1] -diction.get(i)[1])
            Rdis = abs(diction.get(R)[0] -diction.get(i)[0]) +abs(diction.get(R)[1] -diction.get(i)[1])
            # distance가 같은 경우 -> hand
            if Ldis == Rdis:
                if hand == 'right':
                    answer+= "R"
                    R = i
                elif hand == 'left':
                    answer +="L"
                    L = i

            elif Ldis < Rdis:
                answer+= "L"
                L = i
            else:
                answer+= "R"
                R = i

    return (answer)

 

다른사람풀이

def solution(numbers, hand):
    answer = ''
    key_dict = {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 = [1,4,7]
    right = [3,6,9]
    lhand = '*'
    rhand = '#'
    for i in numbers:
        if i in left:
            answer += 'L'
            lhand = i
        elif i in right:
            answer += 'R'
            rhand = i
        else:
            curPos = key_dict[i]
            lPos = key_dict[lhand]
            rPos = key_dict[rhand]
            ldist = abs(curPos[0]-lPos[0]) + abs(curPos[1]-lPos[1])
            rdist = abs(curPos[0]-rPos[0]) + abs(curPos[1]-rPos[1])

            if ldist < rdist:
                answer += 'L'
                lhand = i
            elif ldist > rdist:
                answer += 'R'
                rhand = i
            else:
                if hand == 'left':
                    answer += 'L'
                    lhand = i
                else:
                    answer += 'R'
                    rhand = i

    return answer
def solution(numbers, hand):
    answer = ''
    location = [[3, 1], [0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
    left, right = [3, 0], [3, 2]
    for i in numbers:
        if i % 3 == 1:
            answer += 'L'
            left = location[i]
        elif i % 3 == 0 and i != 0:
            answer += 'R'
            right = location[i]
        else:
            l = abs(location[i][0] - left[0]) + abs(location[i][1] - left[1])
            r = abs(location[i][0] - right[0]) + abs(location[i][1] - right[1])
            if l < r:
                answer += 'L'
                left = location[i]
            elif l > r:
                answer += 'R'
                right = location[i]
            else:
                answer += hand[0].upper()
                if hand == 'right':
                    right = location[i]
                else:
                    left = location[i]                

    return answer

해석

  •  거리를 계산하는 게 핵심이다. ( 좌표를 사용했다 2중 배열)
  •  사전에서 튜플을 사용했다.
  •  
# 경우의수
        #  1 4 7 * 인 경우 -> "L"
        #  3 6 9 # 인 경우 -> "R"
        #  2 5 8 0  (main) 인 경우
            # 거리가 같은 경우
                # hand로 추가
            # 거리가 다른 경우
                # 거리가 짧은 것 선택

    # 거리(main)
        # 거리를 어떻게 구할 것인가
            # L R 값이 재초기화가 계속 되어야한다.
            # 그 좌표를 가지고 x값들 절댓값 차이 + y값들 차이 절댓값

총정리