Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

ol-rlo-zl

[프로그래머스 lv1] 키패드 누르기 - 파이썬(Python) 본문

Programmers(Python)

[프로그래머스 lv1] 키패드 누르기 - 파이썬(Python)

ol-rlo-zl 2023. 4. 11. 17:18

문제

 

프로그래머스

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

programmers.co.kr

풀이

def solution(numbers, hand):
    result = ''
    left = '*'
    right = '#'
    phone = [['1','2','3'],['4','5','6'],['7','8','9'],['*','0','#']]
    def distance(start,end):
        start_x, start_y, end_x, end_y = 0, 0, 0, 0
        for i in range(4):
            for j in range(3):
                if phone[i][j] == start:
                    start_x = i
                    start_y = j
        for i in range(4):
            for j in range(3):
                if phone[i][j] == end:
                    end_x = i
                    end_y = j
        return abs(start_x - end_x) + abs(start_y - end_y)
    for n in numbers:
        n = str(n)
        if n in ['1','4','7']:
            left = n
            result += 'L'
        elif n in ['3','6','9']:
            right = n
            result += 'R'
        else:
            d_l = distance(left,n)
            d_r = distance(right,n)
            if d_l < d_r:
                left = n
                result += 'L'
            elif d_l > d_r:
                right = n
                result += 'R'
            else:
                if hand == 'left':
                    left = n
                    result += 'L'
                else:
                    right = n
                    result += 'R'
    return result

결과