최대 1 분 소요

문제 링크

프로그래머스 - 키패드 누르기

문제 풀이

def dist(a,b):
    return abs(a[0]-b[0]) + abs(a[1]-b[1])

def solution(numbers, hand):
    answer = ''
    locationList = [[1,0],[0,3],[1,3],[2,3],[0,2],[1,2],[2,2],[0,1],[1,1],[2,1]]
    handL = [0,0]
    handR = [2,0]
    
    for num in numbers:
        location = locationList[num]
        if num == 2 or num==5 or num==8 or num==0:
            
            distFromL = dist(handL,location)
            distFromR = dist(handR,location)
        
            if distFromL<distFromR:
                handL = location
                answer = answer+"L"
            elif distFromL>distFromR:
                handR = location
                answer = answer + "R"
            else:
                if hand =="left":
                    handL = location
                    answer = answer+"L"
                else:
                    handR = location
                    answer = answer + "R"
        elif num==1 or num==4 or num==7:
            handL = location
            answer = answer+"L"
        else:
            handR = location
            answer = answer + "R"
          
    
    return answer

댓글남기기