[Python] 프로그래머스 - Level1 - 키패드 누르기

강주형·2022년 8월 8일
0

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

2020 카카오 인턴십

def solution(numbers, hand):
    answer = ''
    current_hand = [10, 12]
    for i, n in enumerate(numbers):
        if n == 0:
            numbers[i] = 11

    for num in numbers:
        dist_1 = [num-1, num+1, num-3, num+3]
        dist_2 = [num-2, num+2, num-4, num+4, num-6, num+6]
        dist_3 = [num-5, num+5, num-7, num+7, num-9, num+9]
        dist_4 = [num-8, num+8, num-10, num+10]
        dist = [0, 0]
        if num in [2, 5, 8, 11]:
            if current_hand[0] in dist_1:
                dist[0] = 1
            elif current_hand[0] in dist_2:
                dist[0] = 2
            elif current_hand[0] in dist_3:
                dist[0] = 3
            elif current_hand[0] in dist_4:
                dist[0] = 4

            if current_hand[1] in dist_1:
                dist[1] = 1
            elif current_hand[1] in dist_2:
                dist[1] = 2
            elif current_hand[1] in dist_3:
                dist[1] = 3
            elif current_hand[1] in dist_4:
                dist[1] = 4

            if dist[0] == dist[1]:
                if hand == 'left':
                    answer += 'L'
                    current_hand[0] = num
                else:
                    answer += 'R'
                    current_hand[1] = num
            elif dist[0] < dist[1]:
                answer += 'L'
                current_hand[0] = num
            else:
                answer += 'R'
                current_hand[1] = num

        elif num in [1, 4, 7]:
            answer += 'L'
            current_hand[0] = num
        else:
            answer += 'R'
            current_hand[1] = num
    return answer

어우 생각보다 너무 오래 걸렸다..

코드가 많이 길어져서 걱정했는데, 다른 사람들 코드 보니까 다 어느정도 길이가 나오는 듯

[*, 0, #] 부분을 [10, 11, 12]로 치환해서
현재 번호에서 +/-로 거리를 계산해서 그 거리를 기준으로 누를 손가락을 결정했음


타인 코드

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

딕셔너리 안에 튜플로 각 번호의 좌표를 저장한 후,
좌우 거리와 상하 거리를 따로 계산후 합쳐서 거리를 구한 것 같음


  1. 좌표나 거리 같은 것을 미리 저장해두는 것도 생각하기
  2. 막막해도 되는 것 먼저 하나씩 해보기
  3. 다음부터 반복되는 코드는 함수화하는 것도 고려하기
profile
Statistics & Data Science

0개의 댓글