- 문제 : https://school.programmers.co.kr/learn/courses/30/lessons/67256

 

해당 숫자와 오른손 엄지와 왼손 엄지 사이의 거리를 구하기 위해서는 각 숫자들의 위치를 좌표로 표현하는 디렉토리가 필요하다고 생각해서 무작정 directory 디렉토리를 만들었다 

그리고 그때그때마다 왼손 엄지와 오른손 엄지의 위치를 저장하는 변수도 따로 만들었고, 2,5,8,0으로 가야할 때 거리를 거리를 계산하기 위해서 left_distance 와 right_distance도 선언했다 

 

프로그래머스 문제 중에는 디렉토리로 푸는 게 많은 것 같다 

 

90%에서 막혀서 다음 블로그를 참고해서 마무리 했다 

참고한 블로그

 

 

- 정답 풀이 :

directory 디렉토리를 함수 바깥에 해야 unboundlocalerror가 발생하지 않는다 

변수명이 문제와 적합하지 않다 

directory = { 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]}

def solution(numbers, hand):
    answer = ''
    left_distance = 0 
    right_distance = 0 
    hands = {'left' : directory['*'], 'right': directory['#']}
    
    while numbers:
        x = numbers.pop(0)
        if x in [1,4,7]:
            answer += 'L'
            hands['left'] = directory[x]
        elif x in [3,6,9]:
            answer += 'R'
            hands['right'] = directory[x]
        elif x in [2,5,8,0]:            
            left_distance = abs(directory[x][0] - hands['left'][0]) + abs(directory[x][1] - hands['left'][1])            
            right_distance = abs(directory[x][0] - hands['right'][0]) + abs(directory[x][1] - hands['right'][1])
            
            if left_distance < right_distance:
                answer += 'L'
                hands['left'] = directory[x]
            elif left_distance > right_distance:
                answer += 'R'
                hands['right'] = directory[x]  
            else:
                if hand == 'right':
                    answer += 'R'
                    hands['right'] = directory[x]
                else:
                    answer += 'L'
                    hands['left'] = directory[x]
            
    return answer

 

- 90점이었던 풀이 

왼손과 오른손이 있는 위치를 숫자가 아닌 좌표로 표현해야한다 

 

def solution(numbers, hand):
    answer = ''
    left_distance = 0 
    right_distance = 0 
    left = 0
    right = 0
    
    directory = {0 : [3,1], 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]}
    
    while numbers:
        x = numbers.pop(0)
        if x in [1,4,7]:
            answer += 'L'
            left = x
        elif x in [3,6,9]:
            answer += 'R'
            right = x 
        elif x in [2,5,8,0]:            
            left_distance = abs(directory[x][0] - directory[left][0]) + abs(directory[x][1] - directory[left][1])            
            right_distance = abs(directory[x][0] - directory[right][0]) + abs(directory[x][1] - directory[right][1])
            
            if left_distance < right_distance:
                answer += 'L'
                left = x
            elif left_distance > right_distance:
                answer += 'R'
                right = x                
            else:
                if hand == 'right':
                    answer += 'R'
                    right = x
                else:
                    answer += 'L'
                    left = x
            
    return answer

+ Recent posts