728x90
반응형
SMALL
프로그래머스 Level 1 키패드 누르기를 파이썬을 통해 풀어보았다.
programmers.co.kr/learn/courses/30/lessons/67256
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
'Problem Solving > programmers' 카테고리의 다른 글
[programmers] Level 1 예산.py (0) | 2021.05.09 |
---|---|
[programmers] Level 1 폰켓몬.java (0) | 2021.05.09 |
[programmers] Level 1 소수 만들기.py (0) | 2021.05.07 |
[programmers] Level 1 음양 더하기.py (0) | 2021.05.07 |
[programmers] Level 1 내적.java (0) | 2021.05.05 |