카카오 인턴쉽

박진은·2023년 3월 19일
0

코테

목록 보기
21/44

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

def find_location(phone,num):
    for i in range(4):
        for e in range(3):
            if phone[i][e] == num:
                return (i,e)
                
def solution(numbers, hand):
    
    answer = ''
    
    left_num = [1,4,7]
    right_num = [3,6,9]
    mid_num = [2,5,8,0]
    
    p = [[1,2,3],
        [4,5,6],
        [7,8,9],
        ['*',0,'#']]
    
    b_right = '#'
    b_left = '*'
    
    for i in numbers:
        
        if i in left_num:
            b_left = i
            answer = answer + 'L'
            
        if i in right_num:
            b_right = i
            answer = answer + 'R'
        
        if i in mid_num:
            right_location = find_location(p,b_right)
            left_location = find_location(p,b_left)
            mid_location = find_location(p,i)
            r_dis = abs(right_location[0] - mid_location[0]) + abs(right_location[1] - mid_location[1])
            l_dis = abs(left_location[0] - mid_location[0]) + abs(left_location[1] - mid_location[1])
            if r_dis > l_dis:
                b_left = i
                answer = answer + 'L'
            elif r_dis < l_dis:
                b_right = i
                answer = answer + 'R'
            else:
                if hand == "right":
                    b_right = i
                    answer = answer + 'R'
                else:
                    b_left = i
                    answer = answer + 'L'
        
        

    return answer

이번 문제는 풀면서 이게 맞나 했는데 이거 이왜에 더 짧게 풀수 있는 방법은 없는거 같다.
숫자의 위치를 딕셔너리로 만들어서 풀면 쉬울거 같기는 하다.

profile
코딩

0개의 댓글