본문 바로가기

Problem Solving/programmers

[programmers] Level 1 키패드 누르기.py

728x90
반응형
SMALL

프로그래머스 Level 1 키패드 누르기를 파이썬을 통해 풀어보았다. 

 

programmers.co.kr/learn/courses/30/lessons/67256

 

코딩테스트 연습 - 키패드 누르기

[1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5] "right" "LRLLLRLLRRL" [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2] "left" "LRLLRRLLLRR" [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] "right" "LLRLLRLLRL"

programmers.co.kr

키패드 누르기.py

 

tomy9729/Algorithm

🐗 내가 직접 작성한 내 코드 🐗. Contribute to tomy9729/Algorithm development by creating an account on GitHub.

github.com

 


Level 1 키패드 누르기

def find_xy(double_array, val) : 
    x=0
    y=0
    for i in range(4) : 
        for j in range(3) : 
            if double_array[i][j] == val :
                x = i
                y = j
    return x,y

def absolute_value(num1, num2) : 
    if num1 > num2 : return num1-num2
    else : return num2 - num1
    
def solution(numbers, hand):
    answer = ''
    key_pad = [[1,2,3],
               [4,5,6],
               [7,8,9],
               ['*',0,'#']]
    left_location = '*'
    right_location = '#'
    
    for i in range(len(numbers)) : 
        if numbers[i] % 3 == 1 :
            answer += "L"
            left_location = numbers[i]
       
        elif numbers[i] % 3 == 2 or numbers[i] == 0 :
            l_loc = find_xy(key_pad,left_location)
            r_loc = find_xy(key_pad,right_location)
            put_loc = find_xy(key_pad,numbers[i])
            l_distance = absolute_value(l_loc[0],put_loc[0])+absolute_value(l_loc[1],put_loc[1])
            r_distance = absolute_value(r_loc[0],put_loc[0])+absolute_value(r_loc[1],put_loc[1])
            if ( l_distance > r_distance) : 
                answer += "R"
                right_location = numbers[i]
            elif (l_distance < r_distance) : 
                answer += "L"
                left_location = numbers[i]
            else : 
                if hand == "right" : 
                    answer += "R"
                    right_location = numbers[i]
                else : 
                    answer += "L"
                    left_location = numbers[i]
        elif numbers[i] % 3 == 0 :
            answer += "R"
            right_location = numbers[i]
                    
    
    return answer

 

728x90
반응형
SMALL